[英]Cannot deserialize the current JSON object error
I'm trying to deserialize the below JSON into a list: 我正在尝试将以下JSON反序列化为列表:
{
character_list: [
{
displayname: "Bob (Server 3)",
name: {
first_lower: "bob",
first: "Bob"
},
id: 123
},
{
displayname: "Bobby (Server 12)",
name: {
first_lower: "bobby",
first: "Bobby"
},
id: 1234
},
{
displayname: "Bobert (Server 9)",
name: {
first_lower: "bobert",
first: "Bobert"
},
id: 12345
},
{
displayname: "Bobostic (Server 1)",
name: {
first_lower: "bobostic",
first: "Bobostic "
},
id: 123456
}
]
}
The JSON is stored in a variable called info
. JSON存储在名为info
的变量中。 I've got the following line: 我有以下几行:
var charList = JsonConvert.DeserializeObject<List<Character>>(info);
But when I try, I get the following error: 但是当我尝试时,出现以下错误:
Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List` because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. 无法将当前的JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. 要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型)数组或列表),可以从JSON对象反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。 Path 'character_list', line 1, position 18. 路径“ character_list”,第1行,位置18。
My Character class: 我的角色班级:
public Character(string json)
{
var jObject = JObject.Parse(json);
var jNow = jObject["character_list"];
Name = (string)jNow.First["name"]["first"];
CharacterId = (int) jNow.First["id"];
DisplayName = (string) jNow.First["displayname"];
}
I've tried searching the site, and most of the answers I saw suggested using the method I'm already using.(ie, Question 1 , Question 2 ) 我已经尝试搜索该站点,并且看到的大多数答案都建议使用我已经使用的方法提出(即, 问题1 , 问题2 )
You can install Newtonsoft JSON and then your class would look like 您可以安装Newtonsoft JSON ,然后您的类如下所示
public class Name
{
[JsonProperty("first_lower")]
public string first_lower { get; set; }
[JsonProperty("first")]
public string first { get; set; }
}
public class CharacterList
{
[JsonProperty("displayname")]
public string displayname { get; set; }
[JsonProperty("name")]
public Name name { get; set; }
[JsonProperty("id")]
public int id { get; set; }
}
public class Character
{
[JsonProperty("character_list")]
public List<CharacterList> character_list { get; set; }
}
Then Deserialize it like this 然后像这样反序列化
string info = File.ReadAllText(YourJSONFile);
var ser = JsonConvert.DeserializeObject<Character>(info);
Isn't the info
variable a JSON Object with one property called character_list
? info
变量不是具有一个名为character_list
属性的JSON对象吗? Shouldn't you be trying to deserialize info.character_list which is the actual list of characters, rather than info. 您不应该尝试反序列化info.character_list,这是实际的字符列表,而不是info。
This explanation fits the error message also. 此说明也适合错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.