如何反序列化这个 JSON?
{"totalResult":3,"categories":[{"1":{"categoryName":"plumbers","categoryIconUrl":"","headingName":"plumbers"}},{"2":{"categoryName":"plumbing parts and accessories","categoryIconUrl":"","headingName":"plumbing parts and accessories"}},{"3":{"categoryName":"plumbing services","categoryIconUrl":"","headingName":"plumbing services"}}]}
所以我已经尝试了几件事......目前我可以获得“totalResult”,但是我无法获得其他数据,这是我用来获取我目前获得的信息的数据合约:
[DataContract]
public class categories_result
{
[DataMember(Name="totalResult")]
public int totalResult {get; set;}
[DataMember(Name="categories")]
public category[] results {get; set;}
}
[DataContract]
public class category
{
[DataMember(Name="categoryName")]
public string categoryName {get; set;}
[DataMember(Name="categoryIconUrl")]
public string categoryIconUrl {get; set;}
[DataMember(Name="headingName")]
public string headingName {get; set;}
}
当涉及到作为结果返回的类别类时,我得到以下信息
- results {TrudonMobile.category[3]} TrudonMobile.category[]
- [0] {WP7proj.category} WP7proj.category
categoryIconUrl Could not evaluate expression string
categoryName Could not evaluate expression string
headingName Could not evaluate expression string
感觉就像我错过了一些东西,但我不确定。
添加:
我想我可能会在实际尝试反序列化的地方添加代码,因为这可能很有用:>
try
{
Stream responseStream = e.Result;
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(categories_result));
categories_result response = (categories_result)ser.ReadObject(responseStream);
}
catch (Exception x)
{
string bob = x.ToString();
return;
}