locked
deserialize json to object c# RRS feed

  • General discussion

  • I'm trying to deserialize json data services recieved from a web server into an object. So far i have just set up a httpwebrequest which gets json fromatted data from the server.

    public void DoHttpWebRequest(string url)
    {
       
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request
    .Method = "GET";
        request
    .BeginGetResponse(new AsyncCallback(onGetResponse), request);
    }

    public void onGetResponse (IAsyncResult asyncResult)
    {
       
    HttpWebRequest myRequest = (HttpWebRequest)asyncResult.AsyncState;
       
    HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(asyncResult);

        using
    (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
       
    {
           
    string results = httpwebStreamReader.ReadToEnd();
           
    Dispatcher.BeginInvoke(() => textBlock5.Text = results);
       
    }
        myResponse
    .Close();
    }

    This returns the following data.

    {"BodyStyle":"Sports","ChassisNumber":19316,"Colour":"Ivory","Condition":"Showroom","Model":"Silver Wraith","Owners":[{"DateBought":"\/Date(-207269643940+0100)\/","DateSold":"\/Date(-113297981580+0100)\/","ID":651,"Owner":{"Address":null,"Decorations":null,"Email":"jvcuejnj.ldmfkiftvh@wx-sts.net","Forename":"Ismael","ID":637,"Mobile":"008547-4461","Surname":"Anderson","Telephone":"366892-9004","Title":"Mr"}}],"RegistrationNumber":"RB4107  ","Year":1909}

    how can I use DataContractJsonSerializer to parse the data into an object with the classes below?

    public class CarOwnershipRecord { 
       
    public int? ID{ get; set; }
       
    public DateTime? DateBought{ get; set; }
       
    public DateTime? DateSold{ get; set; }
       
    public Person Owner{ get; set; }
    }

    public class Car {
       
    public string BodyStyle{ get; set; }
       
    public short? ChassisNumber{ get; set; }
       
    public string Colour{ get; set; }
       
    public string Condition{ get; set; }
       
    public string Model{ get; set; }
       
    public List<CarOwnershipRecord> Owners{ get; set; }
       
    public string RegistrationNumber{ get; set; }
       
    public short Year{ get; set; }
    }

    public class CarPhoto {
       
    public string RegistrationNumber{ get; set; }
       
    public byte[] Photo{ get; set; }
       
    // The Photo field contains the binary contents of an image file
    }
    Monday, April 16, 2012 1:11 AM

All replies

  • Hi Adam,
               DataContractJsonSerializer methods deserialize json and gives you only object. You can not get three different objects,
           Either you have make one class with all above members or parse different json.

       Example:

         [DataContract]
        public class Location
        {
            string _name = String.Empty;
            string _id = String.Empty;
            public Location()
           {
     
           }

             [DataMember]
            public bool Id
            {
                get
                {
                    return _id;
                }
                set
                {
                    _id = value;
                }
            }

             [DataMember]
            public bool Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    _name = value;
                }
            }

             public void setObject(string strJson)
             {


                    MemoryStream readStream = new MemoryStream();                                           //Obtain stream for reading the object
                    DataContractJsonSerializer readSer = new DataContractJsonSerializer(obj.GetType());     //Declare DataContractSerializer object to serialise JSON data
                    byte[] byteRead;
                 
                    //Convert string read into byte array
                    byteRead = System.Text.Encoding.UTF8.GetBytes(strJson);

                    //Write the byte array to the stream
                    readStream.Position = 0;

                    readStream.Write(byteRead, 0, byteRead.Length);
                   
                    obj = readSer.ReadObject(readStream);

                    if (obj != null)
                    {
                      
                       this.Id= ((Location)obj).Id;
                       this.Name = ((Location)obj).Name;
                    }
              }
        }
    Monday, April 16, 2012 7:47 AM
  • Hi, Well this tutorial might help you out in parsing JSON, this tutorial also has some links that would help you doing it,

    http://saad-mehmood.blogspot.com/2013/08/parsing-json-in-windows-phone.html

    Regards,


    Microsoft Student Partner

    Tuesday, August 13, 2013 8:00 AM