如何使用 FastJSON 将 json 解析为字典<字符串、SoilStat>



如何使用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>();

首先,您的JSON是不完整的。我想你的意思是:

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

您可以使用以下代码在fastJSON中解析上述JSON:

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);

相关内容

  • 没有找到相关文章

最新更新