我現在正在做一個windows phone項目,並且需要請求一些json數據的web服務。 如果json結構像 [Dictionary1, Dictionary2, Dictionary3 ] 然後,DataContractJsonSerializer工作正常。 但接下來的請求我會得到一個複雜的json數據,它不起作用。像 [ [Dictionary1], [Dictionary2], [Dictionary3] ]Windows Phone,如何反序列化嵌套的json Array
真實數據是:
[
[{"length":5734.042,"duration":1680,"legs":
[
{"length":685,"duration":555.42,"type":"walk","locs":
[
{"coord":{"x":2546445,"y":6675512},"arrTime":"201203290959","depTime":"201203290959","name":null},
{"coord":{"x":2546433.8,"y":6675498.3},"arrTime":"201203291000","depTime":"201203291000","name":"Teekkaripolku"}
]
},
{"length":4158,"duration":420,"type":"5","code":"2506 2","locs":
[
{"coord":{"x":2546168,"y":6674959},"arrTime":"201203291009","depTime":"201203291009","name":"Alvar Aallon puisto","code":"2222235","shortCode":"E2226","stopAddress":"Otaniementie"},
{"coord":{"x":2546337,"y":6674857},"arrTime":"201203291009","depTime":"201203291009","name":"Dipoli","code":"2222204","shortCode":"E2204","stopAddress":"Otaniementie"}
]
}
]
}],
[{"length":5734.042,"duration":1680,"legs":
[
{"length":685,"duration":555.42,"type":"1", "code":"1111", "locs":
[
{"coord":{"x":2546445,"y":6675512},"arrTime":"201203290952","depTime":"201203290952","name":null},
{"coord":{"x":2546433.8,"y":6675498.3},"arrTime":"201203290953","depTime":"201203290953","name":"Teekkaripolku"}
]
},
{"length":4158,"duration":420,"type":"5","code":"2194 2","locs":
[ {"coord":{"x":2546168,"y":6674959},"arrTime":"201203291002","depTime":"201203291002","name":"Alvar Aallon puisto","code":"2222235","shortCode":"E2226","stopAddress":"Otaniementie"},
{"coord":{"x":2546337,"y":6674857},"arrTime":"201203291002","depTime":"201203291002","name":"Dipoli","code":"2222204","shortCode":"E2204","stopAddress":"Otaniementie"}
]
}
]
}]
]
和類模型是:
[DataContract]
public class RouteList
{
[DataMember]
public List<Route> oneRoute;
}
---
[DataContract]
public class Route
{
[DataMember(Name = "length", IsRequired = true)]
public Double length { get; set; }
[DataMember(Name = "duration", IsRequired = true)]
public Double duration { get; set; }
[DataMember(Name = "legs", IsRequired = true)]
public List<Leg> legs { get; set; }
}
----
[DataContract]
public class Leg
{
[DataMember(Name = "length", IsRequired = true)]
public Double length { get; set; }
[DataMember(Name = "duration", IsRequired = true)]
public Double duration { get; set; }
[DataMember(Name = "type", IsRequired = true)]
public String type { get; set; }
[DataMember(Name = "code", IsRequired = false)]
public String code { get; set; }
[DataMember(Name = "locs", IsRequired = true)]
public List<Loc> locs { get; set; }
[DataMember(Name = "shape", IsRequired = false)]
public String shape { get; set; }
}
-----
[DataContract]
public class Loc
{
[DataMember(Name = "coord", IsRequired = true)]
public String coord { get; set; }
[DataMember(Name = "arrTime", IsRequired = true)]
public String arrTime { get; set; }
[DataMember(Name = "depTime", IsRequired = true)]
public String depTime { get; set; }
[DataMember(Name = "name", IsRequired = true)]
public String name { get; set; }
[DataMember(Name = "code", IsRequired = false)]
public String code { get; set; }
[DataMember(Name = "shortCode", IsRequired = false)]
public String shortCode { get; set; }
[DataMember(Name = "stopAddress", IsRequired = false)]
public String stopAddress { get; set; }
}
和反序列化代碼:
System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(e.Result));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<RouteList>));
result = (List<RouteList>)serializer.ReadObject(mStream);
我已經嘗試過DataContractJsonSerializer和json.NET。但失敗了。最可能的問題是嵌套數組,模型:RouteList。任何人都知道如何解決它?非常感謝。
爲了得到一些幫助,請添加代碼的樣本.. – 2012-03-30 06:11:08
@ Alaa.Ali heyAlaa,我已經更新,添加的類模型和反序列化的代碼。非常感謝 – NOUX 2012-03-31 02:44:06