Answer the question
In order to leave comments, you need to log in
Simple JSON deserialization in C# fails, why?
Hello.
I have this JSON string:
{"id":"3","companies_name":"{EQ\r\n"}
{"id":"6","companies_name":"testName Company"}
{"id":"7","companies_name":"testName Company324324"}
{"id":"8","companies_name":"testName Company"}
{"id":"9","companies_name":"testName Companydfgdfgf"}
{"id":"10","companies_name":"testName Company"}
{"id":"13","companies_name":"testName Company"}
Companies restoredCompanies = JsonConvert.DeserializeObject<Companies>(result.message);
List<Companies> restoredCompanies = JsonConvert.DeserializeObject<List<Companies>>(result.message);
// и так:
Companies[] restoredCompanies = JsonConvert.DeserializeObject<Companies[]>(result.message);
class Companies
{
public string id { get; set; }
public string companies_name { get; set; }
}
Answer the question
In order to leave comments, you need to log in
I have this JSON string:
[
{"id":"3","companies_name":"{EQ\r\n"},
{"id":"6","companies_name":"testName Company"},
{"id":"7","companies_name":"testName Company324324"},
{"id":"8","companies_name":"testName Company"},
{"id":"9","companies_name":"testName Companydfgdfgf"},
{"id":"10","companies_name":"testName Company"},
{"id":"13","companies_name":"testName Company"}
]
JsonConvert.Deserialize<Companies[]>(str)
class Company {
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("companies_name")]
public string Name { get; set; }
}
It can throw an error because it failed to deserialize exactly to the specified class.
I do not like code-first, and in general, de-realization into a class. I am parsing json into a dictionary of dictionaries.
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
Dictionary<string, object> obj = (Dictionary<string, object>)serializer.DeserializeObject(***json***);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question