简体   繁体   English

C#json对象不会反序列化

[英]c# json object won't deserialize

i'm trying to use the nba api to return me a json object. 我正在尝试使用nba api向我返回一个json对象。

{
"resource": "commonteamyears",
"parameters": {
    "LeagueID": "00"
},
"resultSets": [
    {
        "name": "TeamYears",
        "headers": [
            "LEAGUE_ID",
            "TEAM_ID",
            "MIN_YEAR",
            "MAX_YEAR",
            "ABBREVIATION"
        ],
        "rowSet": [
            [
                "00",
                1610612737,
                "1949",
                "2017",
                "ATL"
            ],
            [
                "00",
                1610612738,
                "1946",
                "2017",
                "BOS"
            ],
            [
                "00",
                1610612739,
                "1970",
                "2017",
                "CLE"
            ],
            [
                "00",
                1610612740,
                "2002",
                "2017",
                "NOP"
            ],
            [
                "00",
                1610612741,
                "1966",
                "2017",
                "CHI"
            ],
            [
                "00",
                1610612742,
                "1980",
                "2017",
                "DAL"
            ],
            [
                "00",
                1610612743,
                "1976",
                "2017",
                "DEN"
            ],
            [
                "00",
                1610612744,
                "1946",
                "2017",
                "GSW"
            ],
            [
                "00",
                1610612745,
                "1967",
                "2017",
                "HOU"
            ],
            [
                "00",
                1610612746,
                "1970",
                "2017",
                "LAC"
            ],
            [
                "00",
                1610612747,
                "1948",
                "2017",
                "LAL"
            ],
            [
                "00",
                1610612748,
                "1988",
                "2017",
                "MIA"
            ],
            [
                "00",
                1610612749,
                "1968",
                "2017",
                "MIL"
            ],
            [
                "00",
                1610612750,
                "1989",
                "2017",
                "MIN"
            ],
            [
                "00",
                1610612751,
                "1976",
                "2017",
                "BKN"
            ],
            [
                "00",
                1610612752,
                "1946",
                "2017",
                "NYK"
            ],
            [
                "00",
                1610612753,
                "1989",
                "2017",
                "ORL"
            ],
            [
                "00",
                1610612754,
                "1976",
                "2017",
                "IND"
            ],
            [
                "00",
                1610612755,
                "1949",
                "2017",
                "PHI"
            ],
            [
                "00",
                1610612756,
                "1968",
                "2017",
                "PHX"
            ],
            [
                "00",
                1610612757,
                "1970",
                "2017",
                "POR"
            ],
            [
                "00",
                1610612758,
                "1948",
                "2017",
                "SAC"
            ],
            [
                "00",
                1610612759,
                "1976",
                "2017",
                "SAS"
            ],
            [
                "00",
                1610612760,
                "1967",
                "2017",
                "OKC"
            ],
            [
                "00",
                1610612761,
                "1995",
                "2017",
                "TOR"
            ],
            [
                "00",
                1610612762,
                "1974",
                "2017",
                "UTA"
            ],
            [
                "00",
                1610612763,
                "1995",
                "2017",
                "MEM"
            ],
            [
                "00",
                1610612764,
                "1961",
                "2017",
                "WAS"
            ],
            [
                "00",
                1610612765,
                "1948",
                "2017",
                "DET"
            ],
            [
                "00",
                1610612766,
                "1988",
                "2017",
                "CHA"
            ],
            [
                "00",
                1610610031,
                "1946",
                "1946",
                null
            ],
            [
                "00",
                1610610029,
                "1948",
                "1948",
                null
            ],
            [
                "00",
                1610610025,
                "1946",
                "1949",
                null
            ],
            [
                "00",
                1610610034,
                "1946",
                "1949",
                null
            ],
            [
                "00",
                1610610036,
                "1946",
                "1950",
                null
            ],
            [
                "00",
                1610610024,
                "1947",
                "1954",
                null
            ],
            [
                "00",
                1610610027,
                "1949",
                "1949",
                null
            ],
            [
                "00",
                1610610030,
                "1949",
                "1952",
                null
            ],
            [
                "00",
                1610610033,
                "1949",
                "1949",
                null
            ],
            [
                "00",
                1610610037,
                "1949",
                "1949",
                null
            ],
            [
                "00",
                1610610023,
                "1949",
                "1949",
                null
            ],
            [
                "00",
                1610610026,
                "1946",
                "1946",
                null
            ],
            [
                "00",
                1610610028,
                "1946",
                "1946",
                null
            ],
            [
                "00",
                1610610032,
                "1946",
                "1948",
                null
            ],
            [
                "00",
                1610610035,
                "1946",
                "1946",
                null
            ]
        ]
    }
]

} }

the json it gives me back is giving me a headache using the built in vs paste to class i get this as my classes 使用内置的vs粘贴将我带回来的json让我头疼

public class Parameters
{
    public string LeagueID { get; set; }
}

public class ResultSet
{
    public string name { get; set; }
    public List<string> headers { get; set; }
    public List<List<object>> rowSet { get; set; }
}

public class RootObject
{
    public string resource { get; set; }
    public Parameters parameters { get; set; }
    public List<ResultSet> resultSets { get; set; }
}

now the only thing i want back is the rowset which has the teamID and abbreviaton in it. 现在,我唯一想要返回的是其中具有teamID和abbreviaton的行集。 unfortunately i have no clue how to proceed with the given class. 不幸的是,我不知道如何进行给定的课程。

Something like: 就像是:

var json = File.ReadAllText("my.txt"); // I'm too lazy to escape
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
var data = obj.resultSets.Single(x => x.name == "TeamYears");
var teamIdIndex = data.headers.IndexOf("TEAM_ID");
var abbrIndex = data.headers.IndexOf("ABBREVIATION");
foreach(var row in data.rowSet)
{
    var id = row[teamIdIndex];
    var abbr = row[abbrIndex];
    Console.WriteLine($"{id}: {abbr}");
}

sample output: 样本输出:

1610612737: ATL
1610612738: BOS
1610612739: CLE
...

You can access the rowSet as, 您可以按以下方式访问rowSet:

RootObject obj = new RootObject();
var something = obj.resultSets[0].rowSet;

Well, the format of JSON you have to work with is, ahem, somewhat unorthodox. 好吧,您必须使用的JSON格式有点不合常规。 So you will have to do some manual jiggling. 因此,您将必须进行一些手动摇动。

Assuming you are using JSON.Net, and you loaded all your classes, we just need to add one more: 假设您使用的是JSON.Net,并且加载了所有类,我们只需要再添加一个:

public class NbaRow
{
    public string LeagueId {get;set;}
    public long TeamId {get;set;}
    public string MinYear {get;set;}
    public string MaxYear {get;set;}
    public string Abbreviation {get;set;}
}

Now the code would look something like: 现在代码看起来像这样:

var json = "... json here ...";

var root = JsonConvert.DeserializeObject<RootObject>();
var results = new List<NbaRow>();
foreach(var item in root.resultSets.rowSet)
{
   results.Add(new NbaRow{
    LeagueId = Convert.ChangeType(item[0], typeof(string)),
    TeamId = Convert.ChangeType(item[1], typeof(long)),
    MinYear = Convert.ChangeType(item[2], typeof(string)),
    MaxYear = Convert.ChangeType(item[3], typeof(string)),
    Abbreviation = Convert.ChangeType(item[4], typeof(string))
   });
}

Note: this assumes the order of headers doesn't change. 注意:这假设标题顺序不变。 Use @MarcGravell's answer, if the headers can change. 如果标题可以更改,请使用@MarcGravell的答案。

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

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