私は例のようなjsonを持っており、私はjson.netでC#を使用しています。 このjsonをオブジェクトに逆シリアル化しようとしていますが、動作していません。C#、デシリアライズjson
{
"classes": [{
"id": 1,
"mask": 1,
"powerType": "rage",
"name": "Warrior"
}, {
"id": 2,
"mask": 2,
"powerType": "mana",
"name": "Paladin"
}, {
"id": 3,
"mask": 4,
"powerType": "focus",
"name": "Hunter"
}, {
"id": 4,
"mask": 8,
"powerType": "energy",
"name": "Rogue"
}, {
"id": 6,
"mask": 32,
"powerType": "runic-power",
"name": "Death Knight"
}, {
"id": 12,
"mask": 2048,
"powerType": "fury",
"name": "Demon Hunter"
}]
}
1)私は、クラスを作成しました:
public class ClassJson
{
[JsonProperty(PropertyName = "classes")]
public Class Class { get; set; }
}
2)第二のクラス:
public class Class
{
[JsonProperty(PropertyName = "id", NullValueHandling = NullValueHandling.Ignore)]
public int Id { get; set; }
[JsonProperty(PropertyName = "powerType", NullValueHandling = NullValueHandling.Ignore)]
public string PowerType { get; set; }
[JsonProperty(PropertyName = "name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }
}
を私はJSONを取得し、APIを呼び出すと、私は単純にJsonConvert.DeserializeObject<List<ClassJson>>(json)
を呼び出します。何も起こりません、エラーはありません。
クラスをより良く構成するためのヒントを教えてもらえますか? classes
が配列することになっているので、
与えられたjsonの 'classes'は配列を表します。だから、プロパティを 'public Class [] Classes {get;}として宣言する必要があります。セット; } '。それはうまくいくはずです。 –
JsonConvert.DeserializeObject(json)を試してください。クラスはコレクションプロパティです。 –
私は 'クラス'は 'List'、または別のコレクションタイプでなければならないと思います。それはjsonの配列なので。 –