我不知道这是我还是我得到的 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]"
所以……有什么想法吗?