使用 DataContractJsonSerializer 解析 JSON 时出现异常
本文关键字:异常 JSON DataContractJsonSerializer 解析 使用 | 更新日期: 2023-09-27 18:32:59
我不知道这是我还是我得到的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"
}
}
}
无论如何,为了理智起见,我已经减少了结果的数量,所以我试图为此构建数据契约,我遇到了我以前遇到的一个问题,一些聪明的诡计为我提供了我需要的结果。然而这次它不同...
使用[数据合约]我可以得到总结果,但是,为了得到商家,我不断收到错误......我相信,由于"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"
如果你想要堆栈跟踪,我会发布它,但这是相当大量的无用信息......此外,如果您将数据成员商家更改为字符串,您将看到以下错误:
"System.Collections.Generic.Dictionary`2[System.String,System.Object]"
所以......有什么想法吗?
使用 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);
}
可行的解决方案,但经过一些研究,我发现了以下信息:
看看这个问题:如何在 C# .NET(win7 手机(中使用"DataContractJsonSerializer"读入"嵌套"Json 文件? 问题似乎是
public Dictionary<string, metchant_info> merchants { get; set; }
序列化程序处理字典的方式似乎存在问题:DataContractJsonSerializer 的反序列化问题
有什么方法可以使DataContractJsonSerializer正确序列化字典吗?
希望你能弄清楚。