This is my current code for deserializing with newtonsoft.json
Crop[] crops = JsonConvert.DeserializeObject<Crop[]>(Globals.cropInfoJson.text);
foreach(Crop crop in crops)
{
Debug.Log(crop.name);
if (crop.name == type.ToString())
{
currentStage = crop.currentStage;
maxStage = crop.maxStage;
currentLifeTime = crop.currentLifeTime;
nutritionalValue = crop.nutritionalValue;
timeToGrow = crop.timeToGrow;
}
}
[System.Serializable]
public class Crop
{
public string name;
public float currentStage;
public float maxStage;
public float currentLifeTime;
public float nutritionalValue;
public float timeToGrow;
}
And this is my json:
{
"corn": {
"name": "corn",
"currentStage": 1,
"maxStage": 3,
"currentLifeTime": 0,
"nutritionalValue": 3,
"timeToGrow": 360
},
"carrots": {
"name": "carrots",
"currentStage": 1,
"maxStage": 3,
"currentLifeTime": 0,
"nutritionalValue": 3,
"timeToGrow": 360
}
}
The error I get is: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Crop[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
Any help is greatly appreciated!