[英]How to parse json into a dictionary<string, SoilStat> using FastJSON
如何使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.