json.net

What should `ReadAsAsync<string>` and `ReadAsStringAsync` be used for?

大兔子大兔子 提交于 2019-12-19 14:24:51
问题 What should HttpContentExtensions.ReadAsAsync<string> and HttpContent.ReadAsStringAsync be used for? They would seem to do similiar things but work in curious ways. A couple of tests and their outputs are below. In some cases JsonReaderException are thrown, in some cases, the JSON is output but with additional escape characters. I've ended up using both functions across my code base, but was hoping to align on one if I could understand how they were supposed to work. //Create data and

Unable to deserialize classes with multiple constructors with Json.NET [duplicate]

久未见 提交于 2019-12-19 13:04:19
问题 This question already has answers here : JSON.net: how to deserialize without using the default constructor? (5 answers) Closed 3 years ago . I have a type that I don't control with multiple constructors, equivalent to this one: public class MyClass { private readonly string _property; private MyClass() { Console.WriteLine("We don't want this one to be called."); } public MyClass(string property) { _property = property; } public MyClass(object obj) : this(obj.ToString()) {} public string

Unable to deserialize classes with multiple constructors with Json.NET [duplicate]

萝らか妹 提交于 2019-12-19 13:04:06
问题 This question already has answers here : JSON.net: how to deserialize without using the default constructor? (5 answers) Closed 3 years ago . I have a type that I don't control with multiple constructors, equivalent to this one: public class MyClass { private readonly string _property; private MyClass() { Console.WriteLine("We don't want this one to be called."); } public MyClass(string property) { _property = property; } public MyClass(object obj) : this(obj.ToString()) {} public string

Deserializing JSON result with Json & JavaScriptSerializer

风流意气都作罢 提交于 2019-12-19 10:48:37
问题 here's my problem: I'm trying to deserialize json that hasn't been done by me. The format of the json is as follows: {"responseId":1200, "availableHotels":[ {"processId":"HA-84665605","hotelCode":"UKKTLT","availabilityStatus":"InstantConfirmation",...}, {"processId":"HA-28600965","hotelCode":"UKKTLT","availabilityStatus":"InstantConfirmation",...}, {"processId":"HI-63991185","hotelCode":"UKJOVF","availabilityStatus":"InstantConfirmation",...} ], "totalFound":9, "searchId":"TP-84026455"} And

Unexpected character encountered while parsing API response

痞子三分冷 提交于 2019-12-19 10:24:09
问题 I am getting this exception: Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=Unexpected character encountered while parsing value: {. Path 'outputObject.address', line 17, position 16. when deserializing the response data from an API. (complete exception on the end of the post) CODE return JsonConvert.DeserializeObject(webResponseEntity.ResponseData, typeof(CarLookupResponse)) as CarLookupResponse; MODEL public class CarLookupResponse : ICarLookupResponse { public

Cannot access properties after JSON deserialization into dynamic

别说谁变了你拦得住时间么 提交于 2019-12-19 09:16:23
问题 I'm having some issues accessing dynamic properties after JSON deserialization. Here is the JSON: { "user" : { "511221" :{ "timestamp" : 1403365646384, "version" : -81317051, "email" : "user@name.com", "name" : "My Name", "structures" : [ "structure.62dd1ff1-f96b-22e3-8d4e-22000c20725r" ] } }, } There's actually two problems here. First is that "511221" changes for each user. This is given when authenticating. I can't create a user class and then create another class which name keeps changing

How do deserialize this JSON into an object?

徘徊边缘 提交于 2019-12-19 09:09:42
问题 I'm trying to use JSON.Net to deserialize a JSON object into a C# object. The object I want to create is MonthlyPerformance which contains a list of Type , which contains a list of Categories , which in turn contains a list of Funds . They are defined as: public class MonthlyPerformance { public List<Type> Types { get; set; } } public class Type { public int Id { get; set; } public string CountryId { get; set; } public string Name { get; set; } public List<Category> Categories { get; set; }

How to serialize a collection (with an indexer property) as a dictionary

百般思念 提交于 2019-12-19 08:17:30
问题 I have a class, FooCollection let's say, which implements IEnumerable<Foo> , and also provides an indexer by which one can look up a Foo by its name. Functionally it's read-only as a dictionary. But it's not really a dictionary because users do not get to decide on the keys. Anyway, I want JSON.NET to serialize this object as a JSON dictionary, instead of as an array, which it's doing now. Sticking JsonDictionaryAttribute on it doesn't work: then it does nothing. Clues? 回答1: You're probably

How to serialize a collection (with an indexer property) as a dictionary

纵饮孤独 提交于 2019-12-19 08:17:01
问题 I have a class, FooCollection let's say, which implements IEnumerable<Foo> , and also provides an indexer by which one can look up a Foo by its name. Functionally it's read-only as a dictionary. But it's not really a dictionary because users do not get to decide on the keys. Anyway, I want JSON.NET to serialize this object as a JSON dictionary, instead of as an array, which it's doing now. Sticking JsonDictionaryAttribute on it doesn't work: then it does nothing. Clues? 回答1: You're probably

Web Api incorrectly deserializing a list of enums

一世执手 提交于 2019-12-19 08:01:04
问题 So, I'm using a web API controller to accept JSON requests. It is mapping to a model object that includes a list of enums. The problem I am having is that if the JSON includes an invalid value, it does not seem to be deserializing correctly. I would expect an invalid value to be mapped to the 0 value type in my enum list, however that is not happening. There are 3 primary cases that I've isolated: If the JSON is in the form of ... "MyEnumList":["IncorrectEnum", "One", "Two"] ... The value is