无法使用c#将json结果反序列化为自定义列表对象

时间:2013-11-25 11:53:16

标签: c# json json.net

我有以下JSON:

{"positions":{"position":[{"cost_basis":102.20000,"date_acquired":"2013-11-18T19
:54:13.730Z","id":192,"quantity":2.00000,"symbol":"C"},{"cost_basis":121.50990,"
date_acquired":"2013-11-20T17:43:41.737Z","id":199,"quantity":1.00000,"symbol":"
TSLA"}]}}

我正在使用JSON.NET(Newtonsoft包)来反序列化到我的数据对象。

List<position> position = JsonConvert.DeserializeObject<List<position>>(responsebody);
foreach (position item in listObj)
{
 Console.WriteLine("Test : ", item.id);
}


public class positions
    {
        List<position> position { get; set; }

    }

public class position
    {
        public float cost_basis { get; set; }
        public DateTime date_acquired { get; set; }
        public int id { get; set; }
        public int quantity { get; set; }
        public string symbol { get; set; }

    }

当我尝试反序列化对象时,我收到以下错误。

Unhandled Exception: Newtonsoft.Json.JsonSerializationException: Cannot deserial
ize the current JSON object (e.g. {"name":"value"}) into type 'System.Collection
s.Generic.List`1[ApiPost.position]' because the type requires a JSON array (e.g.
 [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or chang
e the deserialized type so that it is a normal .NET type (e.g. not a primitive t
ype like integer, not a collection type like an array or List<T>) that can be de
serialized from a JSON object. JsonObjectAttribute can also be added to the type
 to force it to deserialize from a JSON object.
Path 'positions', line 1, position 13.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(Js
onReader reader, Type objectType, JsonContract contract, JsonProperty member, Js
onContainerContract containerContract, JsonProperty containerMember, Object exis
tingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInte
rnal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty mem
ber, JsonContainerContract containerContract, JsonProperty containerMember, Obje
ct existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Jso
nReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type
 objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, Jso
nSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSeriali
zerSettings settings)

1 个答案:

答案 0 :(得分:9)

JSON是有效的,但你有一个包含列表的额外类,所以你需要像这样打破这个类:

public class positionsobj
{
    public positionlist positions { get; set; }
}

public class positionlist
{
    public List<position> position { get; set; }
}

public class position
{
    public float cost_basis { get; set; }
    public DateTime date_acquired { get; set; }
    public int id { get; set; }
    public decimal quantity { get; set; }
    public string symbol { get; set; }
}

然后像这样解码它们:

var responsebody = "{\"positions\":{\"position\":[{\"cost\":102.20000,\"date_acquired\":\"2013-11-18T19:54:13.730Z\",\"id\":192,\"quantity\":2.00000,\"symbol\":\"C\"},{\"cost\":121.50990,\"date_acquired\":\"2013-11-20T17:43:41.737Z\",\"id\":199,\"quantity\":1.00000,\"symbol\":\"TSLA\"}]}}";
positionsobj position = JsonConvert.DeserializeObject<positionsobj>(responsebody);

foreach (position item in position.positions.position)
{
     Console.WriteLine("Test : {0}", item.id);
}

但该课程还有一个额外的问题,quantitydecimal而不是int。而且您的Console.Write错过了{0}参数。