0

我不知道这是我还是我得到的 JSON。无论如何,我正在阅读我认为相关的另一篇文章,但不知道当他弄清楚他需要什么来解决更多 json c# 问题时,这些人在说什么

这是我现在得到的 JSON 样本

{
    "totalResult": 2,
    "merchants": {
        "1718167": {
            "merchantRank": "29",
            "merchantName": "bob the builders factory",
            "merchantSlogan": "if he can't fix it we can",
            "merchantBdesc": "",
            "merchantLogo": "pic1.gif",
            "merchantLink": "http:\/\/www.randomlink.com\/",
            "merchantAddress": "place
street St
area
city
1111",
            "merchantLat": "-15.9935425",
            "merchantLng": "58.0836955",
            "merchantGeoLevel": "st",
            "merchantDistance": "0.00",
            "merchantCategories": "builder",
            "merchantEmail": "",
            "merchantUrl": "http:\/\/www.randomlink.com\/",
            "merchantPhone": "0123456789",
            "merchantMobile": "",
            "merchantSrc": "AJF"
        },
        "113711": {
            "merchantRank": "229",
            "merchantName": "A A A Builders",
            "merchantSlogan": "",
            "merchantBdesc": "",
            "merchantLogo": "pic26.gif",
            "merchantLink": "http:\/\/www.randomlink.com\/",
            "merchantAddress": "",
            "merchantLat": "",
            "merchantLng": "",
            "merchantGeoLevel": "",
            "merchantDistance": "0.00",
            "merchantCategories": "Builder",
            "merchantEmail": "here@randomlink.com",
            "merchantUrl": "http:\/\/randomlink.com",
            "merchantPhone": "0123456789",
            "merchantMobile": "",
            "merchantSrc": "GHF"
        }
    }
}

无论如何,为了理智起见,我已经减少了结果的数量,所以我试图为此构建数据合约,但我遇到了以前遇到的问题,一些巧妙的技巧让我得到了我需要的结果。不过这次不一样...

使用 [DataContract] 我可以获得totalResult,但是,为了让商家我不断收到错误......我相信因为“113711”和“113711”是动态的,这会导致创建数据模型出现问题。所以我想也许一本字典可以解决这个问题。但是,每当我尝试它时,我都会出错!

这是我尝试使用的:

[DataContract]
public class result
{
    [DataMember(Name = "totalResult")]
    public string totalResult { get; set; }
    [DataMember(Name = "merchants")]
    public Dictionary<string, metchant_info> merchants { get; set; }
}
[DataContract]
public class metchant_info
{
    [DataMember(Name = "merchantRank")]
    public string merchantRank;
    [DataMember(Name = "merchantName")]
    public string merchantName;
    [DataMember(Name = "merchantSlogan")]
    public string merchantSlogan;
    [DataMember(Name = "merchantBdesc")]
    public string merchantBdesc;
    [DataMember(Name = "merchantLogo")]
    public string merchantLogo;
    [DataMember(Name = "merchantLink")]
    public string merchantLink;
    [DataMember(Name = "merchantAddress")]
    public string merchantAddress;
    [DataMember(Name = "merchantLat")]
    public string merchantLat;
    [DataMember(Name = "merchantLng")]
    public string merchantLng;
    [DataMember(Name = "merchantGeoLevel")]
    public string merchantGeoLevel;
    [DataMember(Name = "merchantDistance")]
    public string merchantDistance;
    [DataMember(Name = "merchantCategories")]
    public string merchantCategories;
    [DataMember(Name = "merchantEmail")]
    public string merchantEmail;
    [DataMember(Name = "merchantUrl")]
    public string merchantUrl;
    [DataMember(Name = "merchantPhone")]
    public string merchantPhone;
    [DataMember(Name = "merchantMobile")]
    public string merchantMobile;
    [DataMember(Name = "merchantSrc")]
    public string merchantSrc;
}

这是背后的代码...

            try
            {
                Stream responseStream = e.Result;
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(result));
                result response = (result)ser.ReadObject(responseStream);
            }
            catch (Exception ex)
            {
                return;
            }

我目前得到的错误是:

"Could not evaluate expression"

如果你想要堆栈跟踪,我会发布它,但它有相当多的无用信息......此外,如果你将数据成员 Merchants 更改为字符串,你将看到以下错误:

"System.Collections.Generic.Dictionary`2[System.String,System.Object]"

所以……有什么想法吗?

4

2 回答 2

3

使用Json.Net,您可以如下动态处理案例

var obj = (JObject)JsonConvert.DeserializeObject(json);

foreach (var item in obj["merchants"])
{
    Console.WriteLine(item.First()["merchantName"]);
}

或者您可以在循环中将每个反序列化为merchant您的对象metchant_info

foreach (var item in obj["merchants"])
{
    var m = JsonConvert.DeserializeObject<metchant_info>(item.First().ToString());
    Console.WriteLine(m.merchantName);
}
于 2012-05-02T12:47:36.023 回答
0

我没有可行的解决方案,但经过一番研究,我发现了以下信息:

看这个问题:如何在 C# .NET(win7 手机)中读入带有“DataContractJsonSerializer”的“嵌套”Json 文件?问题似乎是

public Dictionary<string, metchant_info> merchants { get; set; }

序列化程序处理字典的方式似乎存在问题:DataContractJsonSerializer 的反序列化问题

有什么方法可以使 DataContractJsonSerializer 正确序列化字典?

希望你能弄清楚。

于 2012-05-03T06:54:45.207 回答