我是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