1
\$\begingroup\$

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ wiki.unity3d.com/index.php/SimpleJSON \$\endgroup\$ – Bálint Aug 6 '18 at 9:17
  • \$\begingroup\$ @Bálint: This works but it returns dictionaries instead of objects. That's making it difficult to work with because my JSON can be long. Are there other possible ways to do it? I'll something like Newtonsoft.JsonConvert. \$\endgroup\$ – H. Pauwelyn Aug 6 '18 at 9:34
  • 1
    \$\begingroup\$ As far as I know, doing that requires reflection, which has a serious memory overhead and is really slow. So keep that in mind \$\endgroup\$ – Bálint Aug 6 '18 at 9:38
  • \$\begingroup\$ @Bálint: Yeah, I know how it works. But is there a library that I can download and don't need to write a lot of code? \$\endgroup\$ – H. Pauwelyn Aug 6 '18 at 9:40
2
\$\begingroup\$

It will simply done with JsonUtility.FromJson<JsonClass>(jsonString)

Use [Serializable] before each class for serialization and use Array[] instead of List<>.

According to given json your class should like this

using System;

[Serializable]
public class JsonClass
{
    public Data[] data;
}

[Serializable]
public class Data
{
    public int level;
    public Map map;
    public StartLocation[] startLocations;
    public EndLocation endLocation;
    public PathPoint[] pathPoints;
    public Wave[] waves;
}

[Serializable]
public class Map
{
    public int width;
    public int height;
}

[Serializable]
public class StartLocation
{
    public int x;
    public int y;
    public int z;
}

[Serializable]
public class EndLocation
{
    public int x;
    public int y;
    public int z;
}

[Serializable]
public class PathPoint
{
    public int x;
    public int y;
    public int z;
}

[Serializable]
public class Wave
{

}
\$\endgroup\$
0
\$\begingroup\$

After searching for a while, I've found an asset from the asset store named JSON.NET. Just install this and use this code to convert the JSON code.

using Newtonsoft.Json; // on the top of the file.
JsonConvert.DeserializeObject<Data>(myJsonCode);
\$\endgroup\$
0
\$\begingroup\$

Put a [Serializable] tag on each of your classes that correspond to JSON. This will indicate that these can be serialized.

This might fix the JSONUtility, as I can't see any of the nested arrays that cause the problem in the mentioned link.

\$\endgroup\$
  • \$\begingroup\$ I've tried this but doesn't work also. It returns me a null. \$\endgroup\$ – H. Pauwelyn Aug 6 '18 at 9:31
  • \$\begingroup\$ This answer should work, but the JSON in your question doesn't match your usage in JsonUtility.FromJson<Data>(myJsonCode). Your JSON is an object with a field named data containing an array of Data, not a single Data alone. \$\endgroup\$ – vrostu Dec 14 '18 at 15:48
  • \$\begingroup\$ I dont know why this answer is downvoted. Upvoting. \$\endgroup\$ – LumbusterTick Jul 19 at 8:37
0
\$\begingroup\$

I see that you are using the DotNet 3.5 framework, so I’m guessing it’s Mono framework. If you can upgrade your project to DotNet 4.5 or above, it will allow you to use JSON.NET without issues. As far as I remember, JSON.NET is not supported for mono framework.

If you can not however upgrade your project, you can try using MiniJSON, which is a free JSON parser. It’s easy to serialise and deserialise stuff, and it works well with mono framework. I’m not sure if you will be able to parse the JSON into the object directly though, you might have to assign each field.

\$\endgroup\$
0
\$\begingroup\$

I was also facing problems in JSON parsing. Finally I learnt JSON parsing. Now I can parse any JSON. So I did tutorial on it. I am requesting you to check out. First of all I have covered Newton JSON Plugin and then without plugin using JSON Utility class. Json Parsing in Unity By using Newton json Plugin(JSON .Net) and Jsonutility

Steps for Newton JSON:

  1. Search on Asset store Newton JSON or JSON .Net and install it.
  2. If you get errors, change .Net version to .Net4
  3. Copy your json and go on site of json2csharp and paste there. It will create a json class. Copy json class AND paste it in your class.
  4. Create Object of RootObject class and get data whatever you want.

For JsonUtility follow till steps 3 and then from classes remove get set and just add semicolon with variable and on each class created by json2csharp site write [Serializable]. It will be on top of class.

Hope it will help you.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.