C# 错误:无法将当前JSON对象(例如,{“名称”:“值”)反序列化为类型';System.Collections.Generic.List`1
我正在尝试将API json响应反序列化到列表中。 但我得到了一个错误:无法将当前JSON对象(例如,{“name”:“value”})反序列化为'System.Collections.Generic.List`1'类型 下面是我用来反序列化的代码:C# 错误:无法将当前JSON对象(例如,{“名称”:“值”)反序列化为类型';System.Collections.Generic.List`1,c#,restsharp,C#,Restsharp,我正在尝试将API json响应反序列化到列表中。 但我得到了一个错误:无法将当前JSON对象(例如,{“name”:“value”})反序列化为'System.Collections.Generic.List`1'类型 下面是我用来反序列化的代码: var response = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EndofDay.Insrtumentxxx>>(Content); } 以下是我的js
var response = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EndofDay.Insrtumentxxx>>(Content);
}
以下是我的json api响应:
{\"instrumentxxx\":
[{\"id\":\"B1QH8P2\",
\"APrice\":5.83,
\"BPrice\":6.83,
\"CPrice\":7.83,}]}
我缺少什么?您的JSON响应是数组,并且您已声明为单个对象。您必须将
instrumentxxx声明为列表
这是工作代码
公共类EndofDay
{
公共列表工具xxx{get;set;}
}
公共类仪器XXX
{
公共字符串id{get;set;}
公共双APrice{get;set;}
公共双BPrice{get;set;}
公共双CPrice{get;set;}
}
缺少实际反序列化字符串的代码。您的示例中没有显示列表
,数组/列表中应包含id
、APrice
、BPrice
、CPrice
的复合对象。您现在已经添加了反序列化代码@Ray抱歉我不是c#的天才,我如何更改为数组?是否var response=Newtonsoft.Json.JsonConvert.DeserializeObject(内容)代码>工作?更新了代码,你能把仪器XXX
类放在EndofDay
之外,然后再试一次吗?您将反序列化到哪个对象?是不是JsonConvert.DeserializeObject(jsonString)
?是的,正确,(内容)是我保存json格式的API响应的地方。将Instrumentxxx移到EndofDay类之外,仍然是相同的错误。添加了工作代码,请参阅一次。
{\"instrumentxxx\":
[{\"id\":\"B1QH8P2\",
\"APrice\":5.83,
\"BPrice\":6.83,
\"CPrice\":7.83,}]}
public class EndofDay
{
public List<Instrumentxxx> instrumentxxx { get; set; }
}
public class Instrumentxxx
{
public string id { get; set; }
public double APrice { get; set; }
public double BPrice { get; set; }
public double CPrice { get; set; }
}