我不知道这是我还是我得到的JSON。无论如何,我正在阅读另一篇我认为相关的文章,但当他弄清楚他需要什么时才知道这些人在谈论more json c# issues
这是我现在正在获得的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"
如果你想要堆栈跟踪我会发布它,但是它有相当多的无用信息...另外如果你将datamember Merchants更改为字符串,你会看到以下错误:
"System.Collections.Generic.Dictionary`2[System.String,System.Object]"
所以......有什么想法吗?
答案 0 :(得分: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);
}
答案 1 :(得分:0)
我没有工作解决方案但经过一些研究后,我发现了以下信息:
看看这个问题:How would I read into a 'nested' Json file with 'DataContractJsonSerializer' in C# .NET (win7 phone)?问题似乎是
public Dictionary<string, metchant_info> merchants { get; set; }
Serializer处理词典的方式似乎存在问题:Deserialization problem with DataContractJsonSerializer
Any way to make DataContractJsonSerializer serialize Dictionaries properly?
希望你能搞清楚。