gpt4 book ai didi

C# - 无法反序列化当前的 JSON 数组

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:23 25 4
gpt4 key购买 nike

我有这个代码:

string json2 = vc.Request(model.uri + "?fields=uri,transcode.status", "GET");
var deserial = JsonConvert.DeserializeObject<Dictionary<string, object>>(json2);
var transcode = deserial["transcode"];
var serial = JsonConvert.SerializeObject(transcode);
var deserial2 = JsonConvert.DeserializeObject<Dictionary<string, object>>(serial);
var upstatus = deserial2["status"].ToString();

我从服务器得到的json是:

{
"uri": "/videos/262240241",
"transcode": {
"status": "in_progress"
}
}

在 VS2017 上运行时,它可以工作。

但在 VS2010 上出现以下错误:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

我正在使用 Newtonsoft.Json。

有什么想法吗?

最佳答案

您收到的 json 数据不是 Dictionary<string, object> , 它是一个对象

public class Transcode
{
public string status { get; set; }
}

public class VModel
{
public string uri { get; set; }
public Transcode transcode { get; set; }
}

你可以使用这个对象:

var deserial = JsonConvert.DeserializeObject<VModel>(json2);

代替:

var deserial = JsonConvert.DeserializeObject<Dictionary<string, object>>(json2);

关于C# - 无法反序列化当前的 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49550720/

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