0

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!

NotLunaaa
  • 21
  • 1
  • Does this answer your question? [Cannot deserialize the JSON array (e.g. \[1,2,3\]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly](https://stackoverflow.com/questions/22557559/cannot-deserialize-the-json-array-e-g-1-2-3-into-type-because-type-requ) – Jawad Jun 06 '20 at 22:24

3 Answers3

1

You JSON should be like the below

[
  {
    "name": "corn",
    "currentStage": 1,
    "maxStage": 3,
    "currentLifeTime": 0,
    "nutritionalValue": 3,
    "timeToGrow": 360
  },
  {
    "name": "carrots",
    "currentStage": 1,
    "maxStage": 3,
    "currentLifeTime": 0,
    "nutritionalValue": 3,
    "timeToGrow": 360
  }
]

corn and carrots are array items. Also, your class should be like below.

public class Crop
{
    public string name { get; set; }

    public float currentStage { get; set; }

    public float maxStage { get; set; }

    public float currentLifeTime { get; set; }

    public float nutritionalValue { get; set; }

    public float timeToGrow { get; set; }
}
0

Your Crop class should be like this .

    public class Crop
    {
        public CropInfo corn { get; set; }

        public CropInfo carrots { get; set; }
    }

    public class CropInfo
    {
        public string name { get; set; }

        public float currentStage { get; set; }

        public float maxStage { get; set; }

        public float currentLifeTime { get; set; }

        public float nutritionalValue { get; set; }

        public float timeToGrow { get; set; }
    }

And then

Crop crop = JsonConvert.DeserializeObject<Crop>(Globals.cropInfoJson.text);
0

Your current JSON is a JsonObject, not a JsonArray, notice that it starts with { not [. And you have Crop objects, inside an array of crops, so your JSON should look like an Array of Crops.

Try with this 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
    }
 ]
Lotan
  • 4,078
  • 1
  • 12
  • 30