Views: 52.9K
Replies: 4
Archived
|
How to: Deserialization of nested JSONI am receiving the JSON formatted string from a web service over the Internet. I need to get the array of companies out of this results element and add these to a list of companies, something like List<Company>, where Company is a simple class with Name, City, and State properties. Finally, I am not able to use any third party JSON parsers. What is the best way to get this done? Thanks, Leo Benson, Mar 24, 2011
|
|
Reply 1 public class Result { public string company { get; set; } public string city { get; set; } public string state { get; set; } } public class Company { public int version { get; set; } public string query { get; set; } public int begin { get; set; } public int end { get; set; } public int totalResults { get; set; } public int pageNumber { get; set; } public List<Result> results { get; set; } } public static async Task<Company> GetCompanyInfo() { using (var client = new HttpClient()) { var url = string.Format(""); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.GetAsync(url); if (!response.IsSuccessStatusCode) return null; var result = await response.Content.ReadAsStringAsync(); //De-Serialize var list = JsonConvert.DeserializeObject<Company>(result); /* var js = new DataContractJsonSerializer(typeof(Company)); var ms = new MemoryStream(Encoding.ASCII.GetBytes(result)); var list = (Company)js.ReadObject(ms);*/ return list; } } Mateen Kadwaikar, Apr 11, 2016
|
|
Reply 2So the result set is returned as a named object "Results" as shown above. If I were to call the web service from jQuery AJAX it would be:
var url = "/Companies.svc/GetCompanies"; $(document).ready(function () { $.ajax({ type: "POST", url: url, contentType: "application/json; charset=utf-8", data: "{}", datatype: 'json', timeout: 10000, success: function (data) { alert(data.Results[0].Company); } }); }); //which should return the company name of the first object //you can obviously iterate through the company objects //and pass them to a .NET function in the web service that //takes the properties as parameters and spits out a new //Company object an then add that to a List<Company> //it's an extra step but a solution none the less Kevon Hayes, Sep 09, 2011
|
|
Reply 3 public class results
{ public string company { get; set; } public string City { get; set; } public string State { get; set; } } public class JsonTest { public JsonTest() { results = new List<results>(); } public string version { get; set; } public string query { get; set; } public string begin { get; set; } public string end { get; set; } public string totalResults { get; set; } public string pageNumber { get; set; } public List<results> results { get; set; } JavaScriptSerializer _Serialize = new JavaScriptSerializer(); JsonTest _JsonTest = new JsonTest(); _JsonTest = (JsonTest)_Serialize.Deserialize(jsoN, typeof(JsonTest)); John Prasad, Sep 07, 2011
|
|
Reply 4I have very little experience with JSON, but have you taken a look at JavaScriptSerializer, I think it is found in System.Web.Extensions.dll. It might be what you need.
UPDATE: There are some examples here, that you can take a look at: http://blah.winsmarts.com/2009-12-How_to_parse_JSON_from_C-.aspx http://stackoverflow.com/questions/401756/parsing-json-using-json-net Good Luck! Robert Blixt, Mar 25, 2011
|