如何访问加载到Class中的JSON?

时间:2018-12-16 16:29:45

标签: c# json json.net

因此,在大量研究了使用C#访问JSON的方式之后,我发现了这种方式,我先将JSON数据存储在变量中,然后将该JSON加载到其类模型中。

问题是,我对如何访问C#中的数据(属性)一无所知。如果这是Javascript,我将使用索引器(但是它不能告诉我该类型不允许它)来访问它,或者通过“”来访问它。但这也不起作用,因为它告诉我属性不是在此上下文中定义的(即使它们是在类模型中定义的)。

我想使用Pokemon API。

这是我的类模型(由VS生成),名为PokemonData.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServerExperiment
{
    class PokemonData
    {
        public class Rootobject
        {
            public List<Ability> abilities { get; set; }
            public int Base_experience { get; set; }
            public List<Form> forms { get; set; }
            public List<Game_Indices> game_indices { get; set; }
            public int height { get; set; }
            public List<Held_Items> held_items { get; set; }
            public int id { get; set; }
            public bool is_default { get; set; }
            public string location_area_encounters { get; set; }
            public List<Move> Moves { get; set; }
            public string name { get; set; }
            public int order { get; set; }
            public Species species { get; set; }
            public Sprites sprites { get; set; }
            public List<Stat> stats { get; set; }
            public List<Type> types { get; set; }
            public int weight { get; set; }
        }

        public class Species
        {
            public string name { get; set; }
            public string url { get; set; }
        }

        public class Sprites
        {
            public string back_default { get; set; }
            public object back_female { get; set; }
            public string back_shiny { get; set; }
            public object back_shiny_female { get; set; }
            public string front_default { get; set; }
            public object front_female { get; set; }
            public string front_shiny { get; set; }
            public object front_shiny_female { get; set; }
        }

        public class Ability
        {
            public Ability1 ability { get; set; }
            public bool is_hidden { get; set; }
            public int slot { get; set; }
        }

        public class Ability1
        {
            public string name { get; set; }
            public string url { get; set; }
        }

        public class Form
        {
            public string name { get; set; }
            public string url { get; set; }
        }

        public class Game_Indices
        {
            public int game_index { get; set; }
            public Version version { get; set; }
        }

        public class Version
        {
            public string name { get; set; }
            public string url { get; set; }
        }

        public class Held_Items
        {
            public Item item { get; set; }
            public Version_Details[] version_details { get; set; }
        }

        public class Item
        {
            public string name { get; set; }
            public string url { get; set; }
        }

        public class Version_Details
        {
            public int rarity { get; set; }
            public Version1 Version { get; set; }
        }

        public class Version1
        {
            public string Name { get; set; }
            public string Url { get; set; }
        }

        public class Move
        {
            public Move1 move { get; set; }
            public Version_Group_Details[] Version_group_details { get; set; }
        }

        public class Move1
        {
            public string Name { get; set; }
            public string Url { get; set; }
        }

        public class Version_Group_Details
        {
            public int Level_learned_at { get; set; }
            public Move_Learn_Method Move_learn_method { get; set; }
            public Version_Group Version_group { get; set; }
        }

        public class Move_Learn_Method
        {
            public string Name { get; set; }
            public string Url { get; set; }
        }

        public class Version_Group
        {
            public string Name { get; set; }
            public string Url { get; set; }
        }

        public class Stat
        {
            public int Base_stat { get; set; }
            public int Effort { get; set; }
            public Stat1 stat { get; set; }
        }

        public class Stat1
        {
            public string Name { get; set; }
            public string Url { get; set; }
        }

        public class Type
        {
            public int Slot { get; set; }
            public Type1 type { get; set; }
        }

        public class Type1
        {
            public string Name { get; set; }
            public string Url { get; set; }
        }
    }
}

这是Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ServerExperiment
{
    class Program
    {
        public static async void Main(string[] args)
        {
            PokemonData PokemonModel = new PokemonData();
            Uri Path = new Uri("https://pokeapi.co/api/v2/pokemon/ditto/");
            using (HttpClient Client = new HttpClient())
            {
                PokemonData PokemonInfo = await APIData(Client, Path);
                Console.WriteLine(PokemonInfo);
            }
        }
        public static async Task<PokemonData> APIData(HttpClient Client, Uri Address)
        {
            string JSONData = await Client.GetStringAsync(Address);
            PokemonData ReadyData = JsonConvert.DeserializeObject<PokemonData>(JSONData);
            return ReadyData;
        }
    }
}

如您所见,我要打印身高或体重或其他任何东西。但是当我写PokemonInfo.(something)时,它告诉我在上下文中它是未定义的,即使它是在类中定义的。

2 个答案:

答案 0 :(得分:1)

buffer.memory更改为RootObject。 然后适当删除不必要的内容。

答案 1 :(得分:1)

您已将类声明为嵌套的,并且外部类PokemonData上没有任何属性。例如,这就是为什么编译器在编写PokemonInfo.height时给您一个错误的原因。 height属性位于Rootobject类中,它是PokemonData类中的另一个类。

有两种方法可以解决此问题。您可以:

  1. 更改代码,以便引用PokemonData.Rootobject而不是PokemonData

    public static async void Main(string[] args)
    {
        Uri Path = new Uri("https://pokeapi.co/api/v2/pokemon/ditto/");
        using (HttpClient Client = new HttpClient())
        {
            PokemonData.Rootobject PokemonInfo = await APIData(Client, Path);
    
            Console.WriteLine("Name: " + PokemonInfo.name);
            Console.WriteLine("Species: " + PokemonInfo.species.name);
            Console.WriteLine("Height: " + PokemonInfo.height);
            Console.WriteLine("Weight: " + PokemonInfo.weight);
            Console.WriteLine("Abilities: " + string.Join(", ", PokemonInfo.abilities.Select(a => a.ability.name)));
            // etc.
        }
    }
    
    public static async Task<PokemonData.Rootobject> APIData(HttpClient Client, Uri Address)
    {
        string JSONData = await Client.GetStringAsync(Address);
        PokemonData.Rootobject ReadyData = JsonConvert.DeserializeObject<PokemonData.Rootobject>(JSONData);
        return ReadyData;
    }
    

或者您可以:

  1. 摆脱外部的PokemonData类,只需将您的Rootobject重命名为PokemonData,我相信这就是@bolkay的建议。所以代替这个:

    public class PokemonData
    {
        public class Rootobject
        {
            public List<Ability> abilities { get; set; }
            public int Base_experience { get; set; }
            public List<Form> forms { get; set; }
            public List<Game_Indices> game_indices { get; set; }
            public int height { get; set; }
            public List<Held_Items> held_items { get; set; }
            public int id { get; set; }
            public bool is_default { get; set; }
            public string location_area_encounters { get; set; }
            public List<Move> Moves { get; set; }
            public string name { get; set; }
            public int order { get; set; }
            public Species species { get; set; }
            public Sprites sprites { get; set; }
            public List<Stat> stats { get; set; }
            public List<Type> types { get; set; }
            public int weight { get; set; }
        }
    
        public class Species
        {
            public string name { get; set; }
            public string url { get; set; }
        }
    
        // etc.
    }
    

    您将拥有:

    public class PokemonData
    {
        public List<Ability> abilities { get; set; }
        public int Base_experience { get; set; }
        public List<Form> forms { get; set; }
        public List<Game_Indices> game_indices { get; set; }
        public int height { get; set; }
        public List<Held_Items> held_items { get; set; }
        public int id { get; set; }
        public bool is_default { get; set; }
        public string location_area_encounters { get; set; }
        public List<Move> Moves { get; set; }
        public string name { get; set; }
        public int order { get; set; }
        public Species species { get; set; }
        public Sprites sprites { get; set; }
        public List<Stat> stats { get; set; }
        public List<Type> types { get; set; }
        public int weight { get; set; }
    }
    
    public class Species
    {
        public string name { get; set; }
        public string url { get; set; }
    }
    
    // etc.