發生了什麼,我該如何解決它?無法反序列化當前的JSON對象(例如{「name」:「value」})
API - >https://developers.pipedrive.com/docs/api/v1/
public class user //related
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public bool has_pic { get; set; }
public string pic_hash { get; set; }
public bool active_flag { get; set; }
}
public class related_objects
{
public List<organization> organization { get; set; } // Others things...
public List<person> person { get; set; } // Others things...
public List<user> user { get; set; }
}
public class registros
{
public bool success { get; set; }
public virtual List<Data> data { get; set; }
public virtual related_objects related_objects { get; set; }
public virtual additional_data additional_data { get; set; }
}
當我返回的JSON,它看起來像這樣:
"related_objects": {
"user": {
"478548": {
"id": 478548,
"name": "UserName",
"email": "UserNameMail",
"has_pic": false,
"pic_hash": null,
"active_flag": true
}
"478548": {
"id": 478548,
"name": "UserName2",
"email": "UserName2Mail",
"has_pic": false,
"pic_hash": null,
"active_flag": true
}
}
}
List<Deals.Data> _listRegistrosDeal = new List<Deals.Data>();
JsonTextReader JsonRetorno = new JsonTextReader(new StringReader(wbc.DownloadString(Deals)));
JsonRetorno.SupportMultipleContent = true;
ser.ObjectCreationHandling = ObjectCreationHandling.Replace;
try
{
Deals.registros registros = ser.Deserialize<Deals.registros>(JsonRetorno);
foreach (Deals.Data reg in registros.data)
{
_listRegistrosDeal.Add(reg);
}
}
catch (JsonException ex)
{
Content(ex.Message);
}
List<Deals.Data> _listRegistrosDeal = new List<Deals.Data>();
List<Deals.user> _listuser = new List<Deals.user>();
JsonTextReader JsonRetorno = new JsonTextReader(new StringReader(wbc.DownloadString(Deals)));
JsonRetorno.SupportMultipleContent = true;
var d = wbc.DownloadString(Deals);
ser.ObjectCreationHandling = ObjectCreationHandling.Replace;
try
{
Deals.registros registros = ser.Deserialize<Deals.registros>(JsonRetorno);
foreach (Deals.Data reg in registros.data)
{
_listRegistrosDeal.Add(reg); // This works
}
foreach (Deals.user User in registros.related_objects.user)
{
_listuser.Add(User);
}
}
catch (JsonException ex)
{
Content(ex.Message);
}
我已經嘗試了下面的提示,但他們並沒有解決這個問題:
我不斷收到此錯誤+ EX:
{「不能反序列化JSON當前對象(例如
{\"name\":\"value\"}
)轉換爲類型'System.Collections.Generic.List1[Intranet.Models.Vendas.Deals+user]' because the type requires a JSON array (e.g.
[1,2,3]`)以正確地反序列化。爲了解決這個錯誤或者改變JSON到JSON陣列(例如
[1,2,3]
)或改變反序列化類型,以便它是一個正常的.NET類型(例如不是原始類型像整數,而不是集合類型等的array
或List<T>
),可以從JSON對象反序列化。JsonObjectAttribute
也可以添加到該類型以強制它從JSON對象反序列化。路徑 'related_objects.user.555393',第1行,位置196632.「}
Newtonsoft.Json.JsonException {Newtonsoft.Json.JsonSerializationException}
答案就在錯誤信息中。你試圖將非數組的東西推到一個集合中(在這個例子中,列表爲)。你不能那樣做。 –