2011-10-12 43 views
0

我是Silverlight和.net架构的新手。我正在开发Windows Phone 7项目。 我从服务器收到一些JSON格式的数据。 我从服务器接收webClient界面的回调数据。然而,我不能序列化c#对象中的数据。 我使用下面的代码DeSerializing复杂json wp7

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; 
    } 

是我应该得到的数据格式是这样

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 
      } 
     ] 
    } 
    . 
    . 
    . 

,这是我使用脱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; } 

} 

但在序列化我得到空值。请告诉我哪里可能会出错。

这是数据的类型我从服务器获取

{"schedules":[{"id":3499,"progress":-9,"state":"complete","starts_at":1317945600,"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}]}, 
+1

这可能有助于创建POCO的:HTTP://json2csharp.com/ –

+0

非常感谢德里克... 。 有效。 – Avijeet

回答

1

在我看来像ScheduleResults应该是schedules,而不是results


我的意思是这样的:

public class SchedulesResults 
{ 

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

应该是这样的:

public class SchedulesResults 
{ 

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

HI cjk,我没有得到你的意思是“报告”。 – Avijeet

+0

也提供更多的信息.​​.这是我从服务器{“计划”:[{“id”:3499,“progress”: - 9,“state”:“complete”,“starts_at”: 1317945600 “primary_topic”:{ “ID”:6}, “secondary_topic”:{ “ID”:11}, “得分”:[{ “schedule_id”:3499, “分数”:2 “topic_id”:6} ,{“schedule_id”:3499,“score”:3,“topic_id”:11}]}, – Avijeet

+0

@avijeet:我已经更新了我的意思的完整示例。 – cjk

0

你出现的尝试和反序列化响应为MyCLASS一个实例,但是,(从现有的代码),它看起来像它应该类型为SchedulesResults

+0

我的歉意它是复制粘贴错误。我实际上使用Scheduleresults。我的担心是如果我正在创建正确的班级结构? – Avijeet