【问题标题】:JSON Deserialize ObjectJSON反序列化对象
【发布时间】:2015-04-02 16:35:11
【问题描述】:

JSON 反序列化对象有问题。我得到了这个 json:

{
    "status": "ok",
    "message": "OK",
    "clientData": {
        "id": 1,
        "properties": {
            "imie": "xxx",
            "nazwisko": "xxxx",
            "tel": "5121211",
            "email": "woeee@wwww",
            "pesel": "1111111",
            "kod_pocztowy": "2222",
            "miejscowosc": "eeee",
            "kwota_wnioskowana": 1,
            "dochod": 11,
            "bik": null,
            "firma_nip": "1",
            "komornik": null,
            "oddluzanie": null,
            "leadID": "I1"
        },
        "subcategories": {
            "67": 2001,
            "69": 2003
        }
    },
    "linksData": {
        "3": {
            "linkId": 574,
            "link": "ddd",
            "kartaPryw": 1,
            "produktNazwa": "xxx",
            "alternatives": {
                "114": {
                    "linkId": 114,
                    "link": "xxx",
                    "kartaPryw": 1
                },
                "513": {
                    "linkId": 513,
                    "link": "xxx",
                    "kartaPryw": 1
                },
                "532": {
                    "linkId": 532,
                    "link": "xxx",
                    "kartaPryw": 1
                }
            }
        },
        "12": {
            "linkId": 33,
            "link": "wwww",
            "kartaPryw": 0,
            "produktNazwa": "ee",
            "alternatives": {
                "38": {
                    "linkId": 38,
                    "link": "wwww",
                    "kartaPryw": 0
                },
                "40": {
                    "linkId": 40,
                    "link": "qqqq",
                    "kartaPryw": 0
                },
                "665": {
                    "linkId": 665,
                    "link": "wwww",
                    "kartaPryw": 0
                }
            }
        },
        "14": {
            "linkId": 116,
            "link": "ww",
            "kartaPryw": 0,
            "produktNazwa": "w",
            "alternatives": {
                "123": {
                    "linkId": 123,
                    "link": "ffff",
                    "kartaPryw": 0
                },
                "124": {
                    "linkId": 124,
                    "link": "ffff",
                    "kartaPryw": 0
                },
                "517": {
                    "linkId": 517,
                    "link": "wwww",
                    "kartaPryw": 0
                },
                "519": {
                    "linkId": 519,
                    "link": "ffff",
                    "kartaPryw": 0
                }
            }
        },
        "17": {
            "linkId": 404,
            "link": "ffff",
            "kartaPryw": 0,
            "produktNazwa": "ee",
            "alternatives": []
        },
        "26": {
            "linkId": 135,
            "link": "eeee",
            "kartaPryw": 0,
            "produktNazwa": "eee",
            "alternatives": []
        },
        "27": {
            "linkId": 790,
            "link": "eeee",
            "kartaPryw": 0,
            "produktNazwa": "ee",
            "alternatives": {
                "792": {
                    "linkId": 792,
                    "link": "eeeee",
                    "kartaPryw": 0
                }
            }
        },
        "34": {
            "linkId": 778,
            "link": "eee",
            "kartaPryw": 0,
            "produktNazwa": "eee",
            "alternatives": []
        }
    },
    "currentCartNr": 1
}

此处的格式很糟糕,因此请将此代码粘贴到此处以更好地查看:http://json.parser.online.fr/

这是我的课程

public class Client
{
    public string Status { get; set; }
    public string Message { get; set; }
    public Data clientData { get; set; }
    public Dictionary<string, linksData> linksData;
    public int currentCartNr { get; set; }
}

public class Data
{
    public int id { get; set; }
    public Dictionary<string, string> properties { get; set; }
    public Dictionary<string, int> subcategories { get; set; }
}

public class linksData
{
    public int linkId { get; set; }
    public string link { get; set; }
    public int kartaPryw { get; set; }
    public string produktNazwa { get; set; }
    public Dictionary<string, alternatives> alternatives;
}


public class alternatives
{
    public int linkId { get; set; }
    public string link { get; set; }
    public int kartaPryw { get; set; }
}

而且我无法反序列化我的 JSON,当它尝试反序列化“替代方案”时出现错误。如果我评论这个public Dictionary&lt;string, alternatives&gt; alternatives; 它正在工作,但我需要这个字段。它出什么问题了?

这就是我反序列化它的方式:

var myObject = JsonConvert.DeserializeObject<Client>(get_person);

错误信息:

无法将当前 JSON 数组(例如 [1,2,3])反序列化为“System.Collections.Generic.Dictionary`2[System.String,Linker.Class.alternatives]”类型,因为该类型需要 JSON 对象(例如 {"name":"value"})正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。 JsonArrayAttribute 也可以添加到类型中,以强制它从 JSON 数组中反序列化。

更新

问题解决了。 alternatives [] 已更改为 null 值,并且可以正常工作。

【问题讨论】:

  • 为什么"alternatives": []没有值是空数组,否则是对象。
  • 是的! public Dictionary&lt;string, alternatives&gt; alternatives;更改为public List&lt;alternatives&gt; alternatives;
  • 或"alternatives": []更改"alternatives": {}
  • List 也不起作用。我将尝试将 [] 更改为 {}
  • 我无法将 [] 更改为 {},因为 JSON 会自动进行

标签: c# json deserialization


【解决方案1】:

这似乎是您的 JSON 的问题(我会说)。 当alternatives包含数据时是可以的(它会序列化为字典)但是当它为空时,它会显示:

"alternatives": []

我认为您必须配置解析器,否则会失败。至少对于 Jackson for Java,这是可配置的。你在用哪一个?我想它也可以配置(也许Json.NET?)

【讨论】:

  • 问题是它改变了类型。
  • @sphvn Sphvn 和 lcet 是同一个用户吗?否则:您发表评论的原因是什么?
  • 我也认为这是问题所在,我得到了空白字段,但我不知道如何配置它。
  • @Icet 哪个序列化器?
  • @JanDoggen OP 在问他为什么要Cannot deserialise the current JSON array,这里的答案有点偏离轨道,但涵盖了基本内容,我只是在澄清。
【解决方案2】:

我看到的问题在于您的序列化而不是反序列化。 通过将空值序列化为字典,您不能只是在以后反序列化它 问题是你从哪里得到你的 JSON 文件,是你在序列化它吗?如果是这样,您需要做的就是将您的序列化更改为此

var jsonText=JsonConvert.serialize(JsonObject, new JsonSerializerSettings() {NullValueHandling= NullValueHandling.Ignore });

反序列化与您在代码中所做的相同

【讨论】:

  • 不幸的是,我无法更改 JSON 的序列化,因为它是由另一个程序员 ( PHP ) 制作的,但他将 value 更改为 null 并且没关系。
猜你喜欢
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多