Reputation: 5319
How to convert json into a dictionary using FastJSON. The string(key) is the name of the soil.
Thanks alot!
"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>();
Upvotes: 0
Views: 268
Reputation: 129777
First of all, your JSON is incomplete. I'm assuming you actually meant this:
{
"Soil":
[
{
"name": "Pebbiland",
"retentionrate": 1,
"cost": 100
},
{
"name": "Sandiland",
"retentionrate": 4,
"cost": 500
},
{
"name": "Spongiland",
"retentionrate": 8,
"cost": 1000
}
]
}
You can parse the above JSON in fastJSON using the following code:
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);
If you need it as a dictionary you can convert it like this (assuming all the names are unique):
Dictionary<string, SoilStat> _soilList = root.Soil.ToDictionary(o => o.name);
Upvotes: 0