简体   繁体   English

使用 Newtonsoft.Json 解析 JSON 时出错

[英]Errors parsing JSON using Newtonsoft.Json

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.无法向 Newtonsoft.Json.Linq.JObject 添加属性字符串。 Property with the same name already exists on object.对象上已存在同名属性。

I have no control over the JSON feed and they just added flags1 and flags2 .我无法控制 JSON 提要,他们只是添加了flags1flags2 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.在添加新字段之前,此代码运行良好。

Update:更新:


The first error was caused by using an outdated version of JSON.net.第一个错误是由使用过时版本的 JSON.net 引起的。 There was a built-in version with the CMS system I am using and it was 3.5.我正在使用的 CMS 系统有一个内置版本,它是 3.5。 When I use 4.5 I get a new error:当我使用 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.事实证明,我下面的 JSON 格式与我正在处理的格式不完全相同。 Please note the update.请注意更新。 The error seems to be caused here:错误似乎是在这里引起的:

"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; }
}

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!编辑:看来,至少在最新的 nuget 版本中,JObject.Parse 会跳过错误并正确解析数据! I hope you can make that work for you.我希望你能让它为你工作。

Here's an example of how you can do it:这是一个如何做到这一点的示例:

Response result = JObject.Parse(unfilteredJSONData).ToObject<Response>();

除了Connor Hilarides 的回答之外,它还可以用于包括数组:

JToken resultToken = JToken.Parse(unfilteredJSONData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM