开发者

DeSerializing complex json wp7

开发者 https://www.devze.com 2023-04-12 16:47 出处:网络
I am new to Silverlight and .net architecture. I am working on a Windows Phone 7 project. where i receive some JSON format data from the server.

I am new to Silverlight and .net architecture. I am working on a Windows Phone 7 project. where i receive some JSON format data from the server. I receive the data from the server ine the callback of webClient interface. however i am not able to de serialize the data in c# objects. I am using following code

public void GetData_Completed(object sender, DownloadStringCompletedEventArgs e)
    {

        byte[] encodedString = Encoding.UTF8.GetBytes(e.Result);

        //// Put the byte array into a stream and rewind it to the beginning
        MemoryStream ms = new MemoryStream(encodedString);
        ms.Flush();
        ms.Position = 0;

        // convert json result to model
        Stream stream = ms;
        DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(SchedulesResults));
        SchedulesResults myResults =
        (SchedulesResults)dataContractJsonSerializer.ReadObject(stream);

        var result = myResults;
    }

the data format that i am supposed to get is like this

schedules: [
    {
        id: 2897
        progress: -9
        state: complete
        starts_at: 1315267800
        primary_topic: {
            id: 13
        }
        secondary_topic: {
            id: 9
        }
        scores: [
            {
            schedule_id: 2897
            score: 0
            topic_id: 13
            }
            {
            schedule_id: 2897
            score: 4
            topic_id: 9
            }
        ]
    }
    .
    .
    .

and this is the class that i am using to de serilaize

public class SchedulesResults
{

    /// <summary>
    /// Gets or sets the results.
    /// </summary>
    /// <value>The results.</value>
    public Schedules[] results { get; set; }
}

public class Schedules
{
   int id { get; set; }
   int progress { get; set; }
   string state { get; set; }
   DateTime starts_at { get; set; }
   primary_topic primary_topic_ID { get; set; }
   secondry_topic secondary_topic_ID { get; set; }
   Scores[] scores { get; set; }

}
public class primary_topic
{
    int id { get; set; }
}

public class secondry_topic
{
    int id { get; set; }
}

public class Scores
{           
      int schedule_id{ get; set; }
      int score { get; set; }
      int topic_id { get; set; }

}

but on de serializing i am getting null in the value. please tell me where i might be going wrong.

This is the type of data i am getting from server

{"schedules":[{"id":3499,"progress":-9,"state":"complete","starts_at":131794560开发者_如何转开发0,"primary_topic":{"id":6},"secondary_topic":{"id":11},"scores":[{"schedule_id":3499,"score":2,"topic_id":6},{"schedule_id":3499,"score":3,"topic_id":11}]},


Looks to me like the property in ScheduleResults should be schedules, not results.


I mean this:

public class SchedulesResults 
{ 

    /// <summary> 
    /// Gets or sets the results. 
    /// </summary> 
    /// <value>The results.</value> 
    public Schedules[] results { get; set; } 
} 

Should be this:

public class SchedulesResults 
{ 

    /// <summary> 
    /// Gets or sets the results. 
    /// </summary> 
    /// <value>The results.</value> 
    public Schedules[] schedules { get; set; } 
} 


You're appearing to try and deserialize the response as an instance of MyCLASS but, (from the code available) it looks like it should be of type SchedulesResults.

0

精彩评论

暂无评论...
验证码 换一张
取 消