Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I want to parse JSON object returned from a web method. Please help me to do so.
Posted
Updated 28-Jun-13 9:24am
v2
Comments
Sunasara Imdadhusen 25-Jun-13 7:15am    
What kind of your json object data or structure and what is the meaning of "Handling Multi dimentional JSON Array"

C#
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://Your JSON URL");

                //&tracking_number=" + txtttnumber.Text + "&courier=" + ddlcourier.SelectedValue

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream receiveStream = response.GetResponseStream();
                    StreamReader readStream = null;

                    if (response.CharacterSet == null)
                        readStream = new StreamReader(receiveStream);
                    else
                        readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                    //  List<string> list = new List<string>();
                     //Concert JSON objects in to string

                    string data = readStream.ReadToEnd();

                  //You can also convert string into dictionary

   Dictionary<string,object> dict = new JavaScriptSerializer().Deserialize<Dictionary<string,object>>(data);

//now  manipulate dictionary or string as per your need

}
 
Share this answer
 
v2
Comments
sjelen 25-Jun-13 9:59am    
Good point, OP did not specify from where he will be consuming web service, I just assumed it's from browser.
There are many ways to parse JSON, for example:
http://api.jquery.com/jQuery.parseJSON/[^]
If you need more help you have to provide more details, including some code, how are you invoking web method, how does web method response look like...
 
Share this answer
 
Comments
sunpop 26-Jun-13 0:46am    
I'm getting this Json array as result from a RESTful web method. The JSON array is as follows
{"response_list":[{"id_country":"302","label":"NICARAGUA"},{"id_country":"303","label":"NIGER"},{"id_country":"304","label":"NIG\u00c9RIA"},{"id_country":"305","label":"NIU\u00c9"},{"id_country":"306","label":"NORFOLK, \u00ceLE"},{"id_country":"307","label":"NORV\u00c8GE"},{"id_country":"308","label":"NOUVELLE-CAL\u00c9DONIE"},{"id_country":"341","label":"SAINT-VINCENT-ET-LES GRENADINES"},{"id_country":"342","label":"SALOMON, \u00ceLES"},{"id_country":"343","label":"SAMOA"},{"id_country":"344","label":"SAMOA AM\u00c9RICAINES"},],"response_status":{"message":"Success","status":0}}
I need to take the "label" & populate in a dropdown list. This is what I mean by handling JSON multi dimensional array.
sunpop 26-Jun-13 0:52am    
@Nirav Prabtani
Thank you for the solution. But I could not include namespace of JavaScriptSerializer (ie.,I'm unable to add 'using system.web.script.serialization'. I searched for it's dll & I could not find it)Could you please help.
sjelen 26-Jun-13 4:49am    
JavaScripSerializer is available since .NET 3.5 and you need to reference System.Web.Extensions.dll

You could also use a very popular library Json.NET: http://james.newtonking.com/pages/json-net.aspx
or if you don't want 3rd party library you can use System.Runtime.Serialization.Json.DataContractJsonSerializer class:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx
I parse the JSon object through JQuery as follows:

C#
var jsonArr = $("#jsonOutput").val();
   if (jsonArr.toString() != "")
    {  var stType = JSON.parse(jsonArr2);
       var strType = $('#strType');
       strType.append($('<option></option>').val('0').html('--Select--'));
       for (i = 0; i < stType.response_list.length; i++) {
           strType.append($('<option></option>').val(stType.response_list[i].code).html(stType.response_list[i].label));
       }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900