0

具有相同名称的反序列化对象有问题。它们可以重复,但是它们没有以数组形式格式化,因此我使用的 Newtonsoft.Json 库不允许我从这些对象创建数组。这是我遇到的 JSON 示例:

{
"TESKO": {
    "Id": "19337",
    "Name": "PR3337",
    "Status": "Sold",
    "Code": "GPPD",
    "LastUpdatedDate": "2013-08-16",
    "internalId": "19337"
},
"TESKO": {
    "Id": "19337",
    "Name": "PR-6477",
    "Status": "Sold",
    "Code": "GPPD",
    "LastUpdatedDate": "2013-08-16",
    "internalId": "19337"
},
"BRITISHTOBACCO": {
    "Id": "19337",
    "Name": "PR-4634",
    "Status": "Sold",
    "Code": "GPPD",
    "LastUpdatedDate": "2013-08-16",
    "internalId": "19337"
},
"DDI": {
    "Id": "19337",
    "Name": "PR-6477",
    "Status": "Sold",
    "Code": "GPPD",
    "LastUpdatedDate": "2013-08-16",
    "internalId": "19FF337"
}}

upd:这是我将 JSON 字符串反序列化为的类:

// Generated by Xamasoft JSON Class Generator
// http://www.xamasoft.com/json-class-generator

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication2
{
    public class MyResponse
    {
        [JsonProperty("TESKO")]
        public TESKO[] TESKO { get; set; }

        [JsonProperty("BRITISHTOBACCO")]
        public BRITISHTOBACCO[] BRITISHTOBACCO { get; set; }

        [JsonProperty("DDI")]
        public DDI[] DDI { get; set; }
    }

    public class TESKO : CommonResult
    { }

    public class BRITISHTOBACCO : CommonResult
    { }

    public class DDI : CommonResult
    { }

    public class TP : CommonResult
    { }

    public class CommonResult
    {
        [JsonProperty("Id")]
        public string Id { get; set; }

        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("Status")]
        public string Status { get; set; }

        [JsonProperty("Code")]
        public string Code { get; set; }

        [JsonProperty("LastUpdatedDate")]
        public string LastUpdatedDate { get; set; }

        [JsonProperty("internalId")]
        public string InternalId { get; set; }
    }
}

如何让反序列化器将“TESKO”对象视为数组?

4

2 回答 2

1

那 JSON 是失败的。您收到的 JSON 格式不正确。

在服务器上发出 JSON 的人是否无法更改它以使其格式正确?您没有来自服务器的阵列。您实际拥有的是一个被定义两次的属性(在本例中为“TESKO”)。

看看这个失败的 C# 代码......

String fail;
String fail;

您的 JSON 具有完全相同的功能。就像他们试图定义变量 TESKO 两次一样。

看看这段代码...

var js = "{\"TESKO\": {\"Id\": \"19337\",\"Name\": \"PR3337\",\"Status\": \"Sold\",\"Code\": \"GPPD\",\"LastUpdatedDate\": \"2013-08-16\",\"internalId\": \"19337\"},\"TESKO\": {\"Id\": \"19337\",\"Name\": \"PR-6477\",\"Status\": \"Sold\",\"Code\": \"GPPD\",\"LastUpdatedDate\": \"2013-08-16\",\"internalId\": \"19337\"},\"BRITISHTOBACCO\": {\"Id\": \"19337\",\"Name\": \"PR-4634\",\"Status\": \"Sold\",\"Code\": \"GPPD\",\"LastUpdatedDate\": \"2013-08-16\",\"internalId\": \"19337\"},\"DDI\": {\"Id\": \"19337\",\"Name\": \"PR-6477\",\"Status\": \"Sold\",\"Code\": \"GPPD\",\"LastUpdatedDate\": \"2013-08-16\",\"internalId\": \"19FF337\"}}";

var items = JObject.Parse(js);

foreach (var i in items)
{
   Response.Write("Key: " + i.Key + "<br/>Value: " + i.Value + "<br/><br/>");
}

返回以下输出:

Key: TESKO
Value: { "Id": "19337", "Name": "PR-6477", "Status": "Sold", "Code": "GPPD", "LastUpdatedDate": "2013-08-16", "internalId": "19337" }

Key: BRITISHTOBACCO
Value: { "Id": "19337", "Name": "PR-4634", "Status": "Sold", "Code": "GPPD", "LastUpdatedDate": "2013-08-16", "internalId": "19337" }

Key: DDI
Value: { "Id": "19337", "Name": "PR-6477", "Status": "Sold", "Code": "GPPD", "LastUpdatedDate": "2013-08-16", "internalId": "19FF337" }

我看到的唯一选择是制作客户 JSON 反序列化器......

如何在 JSON.NET 中实现自定义 JsonConverter 以反序列化基类对象列表?

于 2013-11-01T16:10:40.363 回答
0

如果你想将 TESKO 反序列化为一个数组,那么你的 json 应该如下所示:

"TESKOS": [
    {
        "TESKO": {
            "Id": "19337",
            "Name": "PR-6477",
            "Status": "Sold",
            "Code": "GPPD",
            "LastUpdatedDate": "2013-08-16",
            "internalId": "19337"
        }
    },
    {
        "TESKO": {
            "Id": "19337",
            "Name": "PR-6477",
            "Status": "Sold",
            "Code": "GPPD",
            "LastUpdatedDate": "2013-08-16",
            "internalId": "19337"
        }
    }
],
于 2013-11-01T16:17:06.087 回答