gpt4 book ai didi

JSON 到 JSON 数组 无法反序列化当前的 JSON 对象

转载 作者:行者123 更新时间:2023-12-01 23:24:11 25 4
gpt4 key购买 nike

我正在使用 Json.Net 将对象 Json 数据反序列化为对象/集合(列表)

我不确定我做错了什么,我试过这个:

List<LanguageObject> lang = JsonConvert.DeserializeObject<List<LanguageObject>>(jsonData);

并尝试了这个:

LanguageObject.Results lang = JsonConvert.DeserializeObject<LanguageObject.Results>(jsonData);

我收到此错误:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[LanguageObject]' 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 change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'meta', line 1, position 8.

这里是我的 Json:

{"meta":
{
"status":200,
"message":[],
"resultSet":
{"id":"05"},
"pagination":
{
"count":2,
"pageNum":1,
"pageSize":2,
"totalPages":1,
"sort":"ordinal",
"order":"Asc",
"currentUri":null,
"nextUri":null
}
},
"results":
{
"id":0,
"name":
"Language",
"DName":null,
"qvalue":"language",
"language":null,
"mUsageCount":null,
"Ordinal":0,
"items":
[
{
"id":0,
"name":"English",
"DName":"English",
"qvalue":null,
"language":null,
"mUsageCount":null,
"Ordinal":0,
"items":[]
},
{
"id":0,
"name":"Spanish",
"DName":"Spanish;",
"qvalue":null,
"language":null,
"mUsageCount":null,
"Ordinal":0,"
items":[]
}
]
}}

语言对象.cs

   Public class LanguageObject
{
public class ResultSet
{
public string id { get; set; }
}

public class Pagination
{
public int count { get; set; }
public int pageNum { get; set; }
public int pageSize { get; set; }
public int totalPages { get; set; }
public string sort { get; set; }
public string order { get; set; }
public object currentUri { get; set; }
public object nextUri { get; set; }
}

public class Meta
{
public int status { get; set; }
public List<object> message { get; set; }
public ResultSet resultSet { get; set; }
public Pagination pagination { get; set; }
}

public class Item
{
public int id { get; set; }
public string name { get; set; }
public string DName { get; set; }
public object qvalue { get; set; }
public object language { get; set; }
public object mUsageCount { get; set; }
public int Ordinal { get; set; }
public List<object> items { get; set; }
public List<object> __invalid_name__
items { get; set; }
}

public class Results
{
public int id { get; set; }
public string name { get; set; }
public object DName { get; set; }
public string qvalue { get; set; }
public object language { get; set; }
public object mUsageCount { get; set; }
public int Ordinal { get; set; }
public List<Item> items { get; set; }
}

public class RootObject
{
public Meta meta { get; set; }
public Results results { get; set; }
}

}

最佳答案

我没有在您的问题中看到定义的 LanguageObject 类。但是,我确实看到了一个 RootObject 类。根据您发布的内容,您需要像这样反序列化:

RootObject root = JsonConvert.DeserializeObject<RootObject>(jsonData);

然后你可以从RootObject中得到Result:

Results lang = root.Results;

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

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