How to: Deserialization of nested JSON

 
122   96.5
Mar 24, 2011
 

I am receiving the JSON formatted string from a web service over the Internet.
It is roughly formatted like so:

{   "version" : 2,
    "query" : "Companies, CA",
    "begin" : 1,
    "end" : 3,
    "totalResults" : 718,
    "pageNumber" : 0,

    "results" : [ 
          { "company" : "ABC Company Inc.",  "city" : "Sacramento",  "state" : "CA" } ,
          { "company" : "XYZ Company Inc.",  "city" : "San Francisco",  "state" : "CA" } ,
          { "company" : "KLM Company Inc.",  "city" : "Los Angeles",  "state" : "CA" } 
    ]
}

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,




1,128   99.9
Mar 25, 2011
I 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!

50   50
Sep 07, 2011
  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));





68   95.3
Sep 09, 2011
So 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