gpt4 book ai didi

c# - 使用 Json.Net 反序列化时出现错误 "Cannot deserialize the current JSON array"

转载 作者:可可西里 更新时间:2023-11-01 07:54:45 24 4
gpt4 key购买 nike

我有一个字符串:

[
{
"key": "key1",
"value": "{'Time':'15:18:42','Data':'15:18:42'}",
"duration": 5
},
{
"key": "key1",
"value": "{'Time':'15:18:42','Data':'15:18:42'}",
"duration": 5
}
]

我的模型类:

public class CPacket
{
public string key { get; set; }
public string value { get; set; }
public int duration { get; set; }
}

我使用 Json.Net,我想将下面的字符串转换为 Json 对象。

CPacket c = JsonConvert.DeserializeObject<CPacket>(strPostData);

但是报错:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'QuoteAPI.Models.CPacket' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

最佳答案

您的 JSON 表示一组 CPacket 对象,而不仅仅是单个对象。您需要反序列化为一个列表。

List<CPacket> list = JsonConvert.DeserializeObject<List<CPacket>>(strPostData);

关于c# - 使用 Json.Net 反序列化时出现错误 "Cannot deserialize the current JSON array",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28869596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com