【发布时间】: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<string, alternatives> 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<string, alternatives> alternatives;更改为public List<alternatives> alternatives; -
或
"alternatives": []更改"alternatives": {} -
List
也不起作用。我将尝试将 [] 更改为 {} -
我无法将 [] 更改为 {},因为 JSON 会自动进行
标签: c# json deserialization