我是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}]},
这可能有助于创建POCO的:HTTP://json2csharp.com/ –
非常感谢德里克... 。 有效。 – Avijeet