1

まず、JSON の使用は初めてです。ここでの回答を使用して、Json.Net を使用して、データを Pokemon API から C# クラス (Pokemon クラス) に逆シリアル化しました。http://json2csharp.comを使用してクラスを定義すると、次のようになります。

public class Pokemon
{
    public Pokemon(string json)
    {         
        JsonConvert.PopulateObject(json, this, PokeApi.JsonSerializerSettings);
    }
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("evolutions")]
    public Evolutions evolutions { get; set; }

    [JsonProperty("national_id")]
    public int national_id { get; set; }
}

resource_uri、attack stat などの他のプロパティが多数あります。

前述のリンクで提供された回答が述べたように、私は JsonConvert.DeserializeObject(json) を次のように使用しました。

public Pokemon GetPokemon(int nationalId)
{
using (WebClient client = new WebClient())
        {
            var json = client.DownloadString("http://pokeapi.co/api/v1/pokemon/" + nationalId + "/");
            var output = JsonConvert.DeserializeObject<Pokemon>(json);

            return output;
        }
    }

ただし、「タイプにはJSONオブジェクトが必要なため、現在のJSON配列([1,2,3]など)をタイプ 'Evolutions'に逆シリアル化できません...」という例外が引き続き発生します...

まったく同じことを尋ねる他の多くの質問を見つけましたが、上位の回答と混同していました.JsonPropertyを使用することもあれば、JsonConverterを使用することもありました.これらすべてが何を意味するのかについての説明はありません. 両方必要ですか?

編集: サンプル json (呼び出し: http://pokeapi.co/api/v1/pokemon/1/ )

{
  "abilities": [
    {
      "name": "overgrow",
      "resource_uri": "/api/v1/ability/1/"
    },
    {
      "name": "chlorophyll",
      "resource_uri": "/api/v1/ability/2/"
    }
  ],
  "attack": 49,
  "catch_rate": 45,
  "created": "2013-11-02T12:08:25.745455",
  "defense": 49,
  "egg_cycles": 21,
  "egg_groups": [
    {
      "name": "Monster",
      "resource_uri": "/api/v1/egg/1/"
    },
    {
      "name": "Grass",
      "resource_uri": "/api/v1/egg/8/"
    }
  ],
  "ev_yield": "1 Sp Atk",
  "evolutions": {
    "level": 16,
    "method": "level up",
    "resource_uri": "/api/v1/pokemon/2/",
    "to": "Ivysaur"
  },
  "exp": 64,
  "growth_rate": "ms",
  "happiness": 70,
  "height": "2'4",
  "hp": 45,
  "male_female_ratio": "87.5/12.5",
  "modified": "2013-11-02T13:28:04.914889",
  "moves": [
    {
      "learn_type": "other",
      "name": "Tackle",
      "resource_uri": "/api/v1/move/1/"
    },
    {
      "learn_type": "other",
      "name": "Growl",
      "resource_uri": "/api/v1/move/2/"
    },
    {
      "learn_type": "level up",
      "level": 10,
      "name": "Vine whip",
      "resource_uri": "/api/v1/move/3/"
    }
  ],
  "name": "Bulbasaur",
  "national_id": 1,
  "resource_uri": "/api/v1/pokemon/4/",
  "sp_atk": 65,
  "sp_def": 65,
  "species": "seed pokemon",
  "speed": 45,
  "total": 318,
  "types": [
    {
      "name": "grass",
      "resource_uri": "/api/v1/type/5/"
    },
    {
      "name": "poison",
      "resource_uri": "/api/v1/type/8/"
    }
  ],
  "weight": "15.2lbs"
}

進化クラス:

public class Evolutions
{
    public int level { get; set; }
    public string method { get; set; }
    public string resource_uri { get; set; }
    public string to { get; set; }
}
4

1 に答える 1

0

http://pokeapi.co/api/v1/pokemon/19/でhttp://json2csharp.com/を試しましたが、表示されるのは

public class RootObject
{
    //...
    public List<Evolution> evolutions { get; set; }
   //...
}

このかなりのマッシュはあなたのポケモンクラスです. したがって、Evolutions をリストとして宣言する必要があります。

于 2014-02-18T08:13:22.770 に答える