C# 使用 Newtonsoft.Json 解析 JSON 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14131862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Errors parsing JSON using Newtonsoft.Json
提问by whoacowboy
I am getting the following error when I try and parse my JSON using Newtonsoft.Json using
当我尝试使用 Newtonsoft.Json 解析我的 JSON 时出现以下错误
Response result = JsonConvert.DeserializeObject<Response>(unfilteredJSONData);
Can not add property string to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.
无法向 Newtonsoft.Json.Linq.JObject 添加属性字符串。对象上已存在同名属性。
I have no control over the JSON feed and they just added flags1
and flags2
. The duplicate string seems to be causing the error, but I don't have any good idea on how to resolve it. This code was working well until the addition of the new fields.
我无法控制 JSON 提要,他们只是添加了flags1
和flags2
. 重复的字符串似乎导致了错误,但我对如何解决它没有任何好主意。在添加新字段之前,此代码运行良好。
Update:
更新:
The first error was caused by using an outdated version of JSON.net. There was a built-in version with the CMS system I am using and it was 3.5. When I use 4.5 I get a new error:
第一个错误是由使用过时版本的 JSON.net 引起的。我正在使用的 CMS 系统有一个内置版本,它是 3.5。当我使用 4.5 时,我收到一个新错误:
Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.
无法将 Newtonsoft.Json.Linq.JValue 添加到 Newtonsoft.Json.Linq.JObject。
It turns out my JSON below was not exactly the same format as what I am dealing with. Please note the update. The error seems to be caused here:
事实证明,我下面的 JSON 格式与我正在处理的格式不完全相同。请注意更新。错误似乎是在这里引起的:
"flags1": {
"string": "text",
"string": "text"
},
The JSON is:
JSON 是:
{
"result":
{
"lookups":
[
{
"groups":
[
{
"item0": "text",
"item1": "text",
"item2": 0,
"item3": 0,
"item4": 11.5,
"item5": true
},
{
"item6": "text",
"oddName": "text"
},
{
"item7": {
"subitem0": "text",
"subitem1": 0,
"subitem2": true
},
"item8": {
"subitem0": "string",
"subitem1": 0,
"subitem2": true
}
},
{
"url": "http://google.com",
"otherurl": "http://yahoo.com",
"alturllist": [],
"altotherurl": []
},
{},
{
"flags1": {
"string": "text"
},
"flags2": {
"string": "text"
}
}
]
},
{
"groups":
[
{
"item0": "text",
"item1": "text",
"item2": 0,
"item3": 0,
"item4": 11.5,
"item5": true
},
{
"item6": "text",
"oddName": "text"
},
{
"item7": {
"subitem0": "text",
"subitem1": 0,
"subitem2": true
},
"item8": {
"subitem0": "string",
"subitem1": 0,
"subitem2": true
}
},
{
"url": "http://google.com",
"otherurl": "http://yahoo.com",
"alturllist": [],
"altotherurl": []
},
{},
{
"flags1": {
"string": "text",
"string": "text"
},
"flags2": {}
}
]
}
]
}
}
The C# classes are:
C# 类是:
// response
[DataContract]
public class Response
{
[DataMember(Name = "result")]
public Result result { get; set; }
}
[DataContract]
public class Result
{
[DataMember(Name = "lookups")]
public List<Item> lookups { get; set; }
}
[DataContract]
public class Item
{
[DataMember(Name = "groups")]
public List<Dictionary<string, object>> groups { get; set; }
}
I have simplified the JSON and code example for clarity.
为清楚起见,我简化了 JSON 和代码示例。
I included the code aspx to simplify reproducing.
我包含了代码 aspx 以简化再现。
test.aspx
测试.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<code>
<%=response %>
</code></div>
</form>
</body>
</html>
test.aspx.cs
测试文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
public partial class Test : System.Web.UI.Page
{
public string response = string.Empty;
public static string cacheDirPath = HttpRuntime.AppDomainAppPath + "cache\";
private static Logger log = LogManager.GetLogger("productFeed");
protected void Page_Load(object sender, EventArgs e)
{
response = readDataFromFile(cacheDirPath + "test2.json");
Response masheryResult = JsonConvert.DeserializeObject<Response>(response);
}
private string readDataFromFile(string filePath)
{
string JSONData = string.Empty;
try
{
StreamReader myFile = new StreamReader(filePath);
JSONData = myFile.ReadToEnd();
myFile.Close();
}
catch (Exception e)
{
log.Warn(e.Message);
}
return JSONData;
}
}
// response
[DataContract]
public class Response
{
[DataMember(Name = "result")]
public Result result { get; set; }
}
[DataContract]
public class Result
{
[DataMember(Name = "lookups")]
public List<Item> lookups { get; set; }
}
[DataContract]
public class Item
{
[DataMember(Name = "groups")]
public List<Dictionary<string, object>> groups { get; set; }
}
采纳答案by Connie Hilarides
Both the properties in the "flags1" object are named "string", you can't define a property twice.
“flags1”对象中的两个属性都被命名为“string”,一个属性不能定义两次。
Edit: It seems that, at least in the latest nuget version, JObject.Parse skips over the error and properly parses the data! I hope you can make that work for you.
编辑:看来,至少在最新的 nuget 版本中,JObject.Parse 会跳过错误并正确解析数据!我希望你能让它为你工作。
Here's an example of how you can do it:
这是一个如何做到这一点的示例:
Response result = JObject.Parse(unfilteredJSONData).ToObject<Response>();
回答by quckly
In additional to Connor Hilarides answer, it can be used including arrays:
除了 Connor Hilarides 的回答之外,它还可以用于包括数组:
JToken resultToken = JToken.Parse(unfilteredJSONData);