0

I have the following problem: I'm trying to parse a nested JSON using the Unity JsonUtility, but if i'm logging one of the nested parameters i get Null as a result.

Here's the Json:

{
    "basic": {
        "name": "Demo Bow" 
    }, 
    "effect": {
        "damage": {
            "stages": 3,
            "one": 4,
            "two": 10,
            "three": 40
        }
    }
}

And here's my code:

public class Basic
{
    public string name;
}

public class Damage
{
    public int stages;
    public int one;
    public int two;
    public int three;
}

public class Effect
{
    public Damage damage;
}

public class RootObject
{
    public Basic basic;
    public Effect effect;
}

Edit: No its not a duplicate, cause I already removed the "{ get; set; }" And heres the missing code.

public static RootObject CreateFromJSON(string json){

        RootObject weapon = JsonUtility.FromJson <RootObject> (json);


        return weapon;

    }

Thanks for any help

BrainFace
  • 15
  • 5
  • It might help us help you if you include the code that's doing the deserialization.... –  Nov 06 '17 at 16:35

1 Answers1

0

You are missing the [System.Serializable] attribute on your classes, it is needed for JsonUtility to be able to serialize or deserialize your classes.

[System.Serializable]
public class Basic
{
    public string name;
}

[System.Serializable]    
public class Damage
{
    public int stages;
    public int one;
    public int two;
    public int three;
}

[System.Serializable]
public class Effect
{
    public Damage damage;
}

[System.Serializable]
public class RootObject
{
    public Basic basic;
    public Effect effect;
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431