0

FastJSONを使用して json を辞書に変換する方法。文字列(キー)は土の名前です。

どうもありがとう!

    "Soil": [
        {
            "name": "Pebbiland",
            "retentionrate": 1,
            "cost": 100
        },
        {
            "name": "Sandiland",
            "retentionrate": 4,
            "cost": 500
        },
        {
            "name": "Spongiland",
            "retentionrate": 8,
            "cost": 1000
        }


public class SoilStat
{
    public int retentionRate;
    public int cost;
}


Dictionary<string, SoilStat> _soilList = new Dictionary<string, SoilStat>();
4

1 に答える 1

0

まず、JSON が不完全です。私はあなたが実際にこれを意味したと仮定しています:

{
    "Soil": 
    [
        {
            "name": "Pebbiland",
            "retentionrate": 1,
            "cost": 100
        },
        {
            "name": "Sandiland",
            "retentionrate": 4,
            "cost": 500
        },
        {
            "name": "Spongiland",
            "retentionrate": 8,
            "cost": 1000
        }
    ]
}

次のコードを使用して、上記の JSON を fastJSON で解析できます。

public class Root
{
    public List<SoilStat> Soil;
}

public class SoilStat
{
    public string name;
    public int retentionRate;
    public int cost;
}

Root root = fastJSON.JSON.ToObject<Root>(jsonString);

辞書として必要な場合は、次のように変換できます (すべての名前が一意であると仮定します)。

Dictionary<string, SoilStat> _soilList = root.Soil.ToDictionary(o => o.name);
于 2015-12-19T18:11:55.170 に答える