Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited tags
Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61
added 374 characters in body
Source Link
H. Pauwelyn
  • 815
  • 5
  • 17
  • 31

I've this JSON code like below. How do I parse it to the objects on code below?

{
  "data": [
    {
      "level": 1,
      "map": {
        "width": 16,
        "height": 16
      },
      "startLocations": [
        {
          "x": 1,
          "y": 0,
          "z": 1
        }
      ],
      "endLocation": {
        "x": 14,
        "y": 0,
        "z": 14
      },
      "pathPoints": [
        {
          "x": 1,
          "y": 0,
          "z": 1
        },
        {
          "x": 2,
          "y": 0,
          "z": 1
        }
      ],
      "waves": []
    }
  ]
}
public class Data
{
    public Level[] levels;
}

public class Level
{
    public int level;
    public Map map;
    public List<Vector3> startLocations;
    public Vector3 endLocation;
    public List<Vector3> pathPoints;
    public List<Wave> waves;
}

public class Map
{
    public int width;
    public int height;
}

public class Wave
{ }

I've tried all this but nothing doesn't work:

  • Using JsonConvert but can't install it. I've got this error:

    Could not install package Newtonsoft.Json 11.0.2. You are trying to install this package into a project that targets .NETFramework,Version=v3.5,Profile=Unity Subset v3.5, but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

  • Using next code but this always returns null.

      JsonUtility.FromJson<Data>(myJsonCode)
    

    I know this from this answer on the Unity forum.

    That's not possible with Unity's JsonUtility. You have to understand that the JsonUtility is not a general purpose JSON serializer / parser. It is build on top of Unity's serialization logic.

Are there other ways to parse JSON to objects in Unity?

I've this JSON code like below. How do I parse it to the objects on code below?

{
  "data": [
    {
      "level": 1,
      "map": {
        "width": 16,
        "height": 16
      },
      "startLocations": [
        {
          "x": 1,
          "y": 0,
          "z": 1
        }
      ],
      "endLocation": {
        "x": 14,
        "y": 0,
        "z": 14
      },
      "pathPoints": [
        {
          "x": 1,
          "y": 0,
          "z": 1
        },
        {
          "x": 2,
          "y": 0,
          "z": 1
        }
      ],
      "waves": []
    }
  ]
}
public class Data
{
    public Level[] levels;
}

public class Level
{
    public int level;
    public Map map;
    public List<Vector3> startLocations;
    public Vector3 endLocation;
    public List<Vector3> pathPoints;
    public List<Wave> waves;
}

public class Map
{
    public int width;
    public int height;
}

public class Wave
{ }

I've tried all this but nothing doesn't work:

  • Using JsonConvert but can't install it.

  • Using next code but this always returns null.

      JsonUtility.FromJson<Data>(myJsonCode)
    

    I know this from this answer on the Unity forum.

    That's not possible with Unity's JsonUtility. You have to understand that the JsonUtility is not a general purpose JSON serializer / parser. It is build on top of Unity's serialization logic.

Are there other ways to parse JSON to objects in Unity?

I've this JSON code like below. How do I parse it to the objects on code below?

{
  "data": [
    {
      "level": 1,
      "map": {
        "width": 16,
        "height": 16
      },
      "startLocations": [
        {
          "x": 1,
          "y": 0,
          "z": 1
        }
      ],
      "endLocation": {
        "x": 14,
        "y": 0,
        "z": 14
      },
      "pathPoints": [
        {
          "x": 1,
          "y": 0,
          "z": 1
        },
        {
          "x": 2,
          "y": 0,
          "z": 1
        }
      ],
      "waves": []
    }
  ]
}
public class Data
{
    public Level[] levels;
}

public class Level
{
    public int level;
    public Map map;
    public List<Vector3> startLocations;
    public Vector3 endLocation;
    public List<Vector3> pathPoints;
    public List<Wave> waves;
}

public class Map
{
    public int width;
    public int height;
}

public class Wave
{ }

I've tried all this but nothing doesn't work:

  • Using JsonConvert but can't install it. I've got this error:

    Could not install package Newtonsoft.Json 11.0.2. You are trying to install this package into a project that targets .NETFramework,Version=v3.5,Profile=Unity Subset v3.5, but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

  • Using next code but this always returns null.

      JsonUtility.FromJson<Data>(myJsonCode)
    

    I know this from this answer on the Unity forum.

    That's not possible with Unity's JsonUtility. You have to understand that the JsonUtility is not a general purpose JSON serializer / parser. It is build on top of Unity's serialization logic.

Are there other ways to parse JSON to objects in Unity?

Source Link
H. Pauwelyn
  • 815
  • 5
  • 17
  • 31

Parse JSON in Unity

I've this JSON code like below. How do I parse it to the objects on code below?

{
  "data": [
    {
      "level": 1,
      "map": {
        "width": 16,
        "height": 16
      },
      "startLocations": [
        {
          "x": 1,
          "y": 0,
          "z": 1
        }
      ],
      "endLocation": {
        "x": 14,
        "y": 0,
        "z": 14
      },
      "pathPoints": [
        {
          "x": 1,
          "y": 0,
          "z": 1
        },
        {
          "x": 2,
          "y": 0,
          "z": 1
        }
      ],
      "waves": []
    }
  ]
}
public class Data
{
    public Level[] levels;
}

public class Level
{
    public int level;
    public Map map;
    public List<Vector3> startLocations;
    public Vector3 endLocation;
    public List<Vector3> pathPoints;
    public List<Wave> waves;
}

public class Map
{
    public int width;
    public int height;
}

public class Wave
{ }

I've tried all this but nothing doesn't work:

  • Using JsonConvert but can't install it.

  • Using next code but this always returns null.

      JsonUtility.FromJson<Data>(myJsonCode)
    

    I know this from this answer on the Unity forum.

    That's not possible with Unity's JsonUtility. You have to understand that the JsonUtility is not a general purpose JSON serializer / parser. It is build on top of Unity's serialization logic.

Are there other ways to parse JSON to objects in Unity?