2

如何反序列化这个 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;
            }
4

2 回答 2

2

使用Json.Net

string 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""
            }
        }
    ]
}
";

dynamic jObj = JsonConvert.DeserializeObject(json);

for (int i = 0; i < jObj.categories.Count; i++)
{
    Console.WriteLine(jObj.categories[i][(i+1).ToString()].categoryName);
}

//或者

JObject jObj = (JObject)JsonConvert.DeserializeObject(json);

for (int i = 0; i < jObj["categories"].Count(); i++)
{
    Console.WriteLine(jObj["categories"][i][(i+1).ToString()]["categoryName"]);
}
于 2012-04-26T09:38:43.593 回答
1

Try json.newtonsoft library

       string data = "@:{"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"}}]} ";


        JObject o = JObject.Parse(data);

        JArray alldata = (JArray)(o["category"]);

to access the members in the category class

       JObject first = (JObject)category[1];
于 2012-04-26T09:21:20.187 回答