我需要使用Newtonsoft.Json将JSON数组转换为C#类,但我不知道如何做到这一点。
我已经试过用
public LeagueInfo[] leagueinfo { get; set; }
字符串
和
public List<LeagueInfo> leagueinfo { get; set; }
型
我现在拥有的是:
public class League
{
public LeagueInfo[] leagueinfo { get; set; }
}
public class LeagueInfo
{
public string queueType { get; set; }
public string summonerName { get; set; }
public bool hotStreak { get; set; }
public int wins { get; set; }
public bool veteran { get; set; }
public int losses { get; set; }
public string rank { get; set; }
public string tier { get; set; }
public bool inactive { get; set; }
public bool freshBlood { get; set; }
public string leagueId { get; set; }
public string summonerId { get; set; }
public int leaguePoints { get; set; }
}
league = JsonConvert.DeserializeObject<League>(data);
的字符串
当我尝试这样做时,我得到了这个错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'League' 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<T> 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.'
型
我的JSON字符串
[
{
"queueType": "RANKED_SOLO_5x5",
"summonerName": "R2FTW",
"hotStreak": false,
"wins": 76,
"veteran": true,
"losses": 97,
"rank": "II",
"tier": "BRONZE",
"inactive": false,
"freshBlood": false,
"leagueId": "2a74bbd0-20ba-11e9-9c10-d4ae52a70a5a",
"summonerId": "n9Sw-4lZHg5Cd2oeMxe8dj9WGv1XQS3GZEMPX1VZHgVH5w",
"leaguePoints": 75
},
{
"queueType": "RANKED_FLEX_SR",
"summonerName": "R2FTW",
"hotStreak": false,
"wins": 0,
"veteran": false,
"losses": 12,
"rank": "IV",
"tier": "IRON",
"inactive": false,
"freshBlood": false,
"leagueId": "b75d4e60-2234-11e9-a815-d4ae527982aa",
"summonerId": "n9Sw-4lZHg5Cd2oeMxe8dj9WGv1XQS3GZEMPX1VZHgVH5w",
"leaguePoints": 0
}
]
型
我该怎么做?
2条答案
按热度按时间3okqufwl1#
试试这个:
字符串
yb3bgrhw2#
问题是JSON解析器需要一个名称。我通过如下方式将名称添加到输出中来完成我的工作:
字符串
当然,这是一个有点黑客,但如果你没有收到更好的格式化数据,这可能是解决方案。