简体   繁体   English

如何将没有名称的 json 数组反序列化为 C# 上的类

[英]How to deserialize a json array without name to a class on C#

I need to deserialize a json array into a C# class using Newtonsoft.Json, but i dont know how to do it.我需要使用 Newtonsoft.Json 将 json 数组反序列化为 C# 类,但我不知道该怎么做。

i have already tried using我已经尝试过使用

public  LeagueInfo[] leagueinfo { get; set; }

and

public  List<LeagueInfo> leagueinfo { get; set; }

What i have now is: - Classes我现在拥有的是: - 课程

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; }
}

Code代码

league = JsonConvert.DeserializeObject<League>(data);

When i try to do that i get this error:当我尝试这样做时,我收到此错误:

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.'

My JSON String is我的 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
    }
]

Sorry if this is a dumb question, i'm new to programing in general.对不起,如果这是一个愚蠢的问题,我一般是编程新手。 How do i do that?我怎么做?

Try this:尝试这个:

League league = new League()
{
    leagueinfo = JsonConvert.DeserializeObject<LeagueInfo[]>(data)
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM