簡體   English   中英

C#JSON返回列表中的列表

[英]C# JSON Returns a List Inside List

public class Entry
{
    public string playerOrTeamId { get; set; }
    public string playerOrTeamName { get; set; }
    public string division { get; set; }
    public int leaguePoints { get; set; }
    public int wins { get; set; }
    public int losses { get; set; }
    public bool isHotStreak { get; set; }
    public bool isVeteran { get; set; }
    public bool isFreshBlood { get; set; }
    public bool isInactive { get; set; }
}

public class SummonerId
{
    public string name { get; set; }
    public string tier { get; set; }
    public string queue { get; set; }
    public List<Entry> entries { get; set; }
}

public class RootObject
{
    public List<SummonerId> Summoner_Id { get; set; }
}

我已經使用Json2csharp.com生成了該類

如果班級有1個列表,我可以毫無問題地訪問數據。

但是用這個類生成了2個列表。 我想我現在想得太多了,變得非常困惑。

我如何反序列化此課程

string url = json.ToString();

var root = JsonConvert.DeserializeObject<RootObject>(url):

Summoner_Id返回為null

var id = root.Summoner_Id;

root也返回null

我該如何解決? 請幫助或指出正確的方向!

這個例子對我有用:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Test {
    static class Program {
        static void Main() {

            string json = @" {
    ""Summoner_Id"": [{
        ""name"": ""Fiora's Inquisitors"",
        ""tier"": ""GOLD"",
        ""queue"": ""RANKED_SOLO_5x5"",
        ""entries"": [{
            ""playerOrTeamId‌​"": ""585709"",
            ""playerOrTeamName"": ""AP Ezreal Mid"",
            ""division"": ""IV"",
            ""leaguePoints"": 61,
            ""wins"": 175,
            ""losses"": 158,
            ""isHotStreak"": false,
            ""isVeteran"": false,
            ""isFreshBlood"": false,
            ""isInactive"": false
        }]
    }]
 }";

            var root = JsonConvert.DeserializeObject<RootObject>(json);
            Console.WriteLine(root.Summoner_Id);
        }
    }

    public class Entry {
        public string playerOrTeamId { get; set; }
        public string playerOrTeamName { get; set; }
        public string division { get; set; }
        public int leaguePoints { get; set; }
        public int wins { get; set; }
        public int losses { get; set; }
        public bool isHotStreak { get; set; }
        public bool isVeteran { get; set; }
        public bool isFreshBlood { get; set; }
        public bool isInactive { get; set; }
    }

    public class SummonerId {
        public string name { get; set; }
        public string tier { get; set; }
        public string queue { get; set; }
        public List<Entry> entries { get; set; }
    }

    public class RootObject {
        public List<SummonerId> Summoner_Id { get; set; }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM