Parse JSON data in unity3d

In this article, I will explain how to parse JSON data in Unity3d. I will use SimpleJson library to parse the JSON data in this article.

In this tutorial, I will use below mentioned JSON data for loading and parsing:

You can access the above mentioned JSON data over following link:

http://gyanendushekhar.com/wp-content/uploads/2016/06/Department.txt

In above mentioned example of JSON, I have added 2 entries for Department node.  In this example, I will retrieve all the values associated in JSON data using SimpleJSON library.

You can download updated SimpleJSON library here: http://wiki.unity3d.com/index.php/SimpleJSON#Download

  • Copy SimpleJSON.cs file in the plugin folder of your unity project
  • add SimpleJSON namespace in your script file.
  • Copy SharpZipLib.dll also in the Plugin folder, if you want to use compression feature, otherwise ignore it.

Store JSON from URL mentioned above and store it in a string type variable.

Parse JSON string and retrieve values inside:

Either you can use name of the node or its position in the JSON. Following two lines of code will give the same result.

Complete C# script for this example:

Output:

Parse json data in unity3d

That’s it for the article – “Parse JSON data in unity3d”. Thanks for reading.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

13 thoughts on “Parse JSON data in unity3d

  • February 16, 2017 at 7:16 am
    Permalink

    Hi, I’m using your code but I’m getting an error the thing it’s that I’m using a .json file and you have a .txt that’s the only thing I see different, what do you think about that?

    Reply
    • February 16, 2017 at 9:51 pm
      Permalink

      I tried with the same example with .json file extension and it is working fine. If you are loading a json file from local drive, then use given line of code.
      WWW www = new WWW (“file:///C://file path…..”);
      if you still get the error, Please share it.

      Reply
  • February 16, 2017 at 11:09 am
    Permalink

    Hello, if i have a json data like this:
    {“0”:{“180947408”:[“D7207″,-28.555801,154.46766662598,1487154089408,15500,”A333″,372,295,”ADSB”,”9M-XBB”,”NZAA”,”YBGC”,”D7″],

    How should i parse it in unity?
    Thanks.

    Reply
    • February 16, 2017 at 10:07 pm
      Permalink

      Let’s take your example to parse: {“0”:{“180947408”:[“D7207″,-28.555801,154.46766662598,1487154089408,15500,”A333″,372,295,”ADSB”,”9M-XBB”,”NZAA”,”YBGC”,”D7″]}}

      1. First Parse it and create object of JSONNode as given in the tutorial.
      JSONNode jsonNode = SimpleJSON.JSON.Parse(jsonData);
      Debug.Log (“Data ” + jsonNode[0].ToString());
      2. Now Process the jsonNode object.
      JSONNode jsonNode2 = SimpleJSON.JSON.Parse(jsonNode[0].ToString());
      Debug.Log (“Data ” + jsonNode2[0].ToString());
      It will give you the inner data within json file.
      Parse JSON in unity3d - Result

      Reply
      • July 22, 2018 at 10:00 am
        Permalink

        May i know how can i loop through the inner values . Thank you

        Reply
        • July 26, 2018 at 7:39 pm
          Permalink

          try this.

          int count = 0;
          while(jsonNode["Department"][count]!=null)
          {
          Debug.Log (jsonNode["Department"][count].ToString());
          count++;
          }

          Reply
  • February 16, 2017 at 11:16 pm
    Permalink

    Hello again, I’m getting the .json file from an url, and I’m getting this error:
    NotImplementedException: null
    SimpleJSON.JSONNode.Numberize (System.String token) (at Assets/Plugins/SimpleJSON.cs:315)
    SimpleJSON.JSONNode.AddElement (SimpleJSON.JSONNode ctx, System.String token, System.String tokenName, Boolean tokenIsString) (at Assets/Plugins/SimpleJSON.cs:329)
    SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/Plugins/SimpleJSON.cs:441)
    SimpleJSON.JSON.Parse (System.String aJSON) (at Assets/Plugins/SimpleJSON.cs:1303)
    ParseUsingSimpleJson+c__Iterator0.MoveNext () (at Assets/scripts/ParseUsingSimpleJson.cs:24)
    UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

    AND I’m using this code:
    using UnityEngine;
    using System.Collections;
    using SimpleJSON;

    public class ParseUsingSimpleJson : MonoBehaviour
    {
    string jsonData;

    // Use this for initialization
    IEnumerator Start()
    {
    // implememt WWW to get json data from any url
    string url = “https://navarcito.cfapps.io/user.json”;
    WWW www = new WWW(url);
    yield return www;

    // store text in www to json string
    if (www.error == null)
    {
    jsonData = http://www.text;
    }

    // use simpleJSON to get values stored in JSON data for different key value pair
    JSONNode jsonNode = SimpleJSON.JSON.Parse(jsonData);
    Debug.Log(“Data”+jsonNode[0].ToString());

    // get values at the nodes, to get values at node either use the name directly or the position number of the node
    // here instead of “Department”, you can also write jsonNode[0][0]

    // get individual values from Department 0

    }
    }

    This works fine with your url, but with my url doesn’t work, I don’t see what wrong. Thanks for your help

    Reply
    • February 16, 2017 at 11:59 pm
      Permalink

      Angelica – I tried the given code. The given code is working for me.
      Can you check what is value of jsonData in the given code. It should not be null.


      string url = "https://navarcito.cfapps.io/user.json";
      WWW www = new WWW (url);

      yield return www;

      // store text in www to json string
      if (www.error == null) {
      jsonData = www.text;
      }
      JSONNode jsonNode = SimpleJSON.JSON.Parse(jsonData);
      Debug.Log ("Data " + jsonNode[0].ToString());

      Reply
  • February 17, 2017 at 12:29 am
    Permalink

    Hi, I print the jsonData and it returns the value that i send like this
    jsonData[{“idUsuario”:1,”username”:”lunaangelica5″,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_ADMIN”},{“idUsuario”:2,”username”:”leomax”,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_DISEÑADOR”},{“idUsuario”:3,”username”:”osma”,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_ORTOPEDISTA”},{“idUsuario”:4,”username”:”asistente”,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_ASISTENTE”},{“idUsuario”:5,”username”:”radiologo”,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_RADIOLOGO”},{“idUsuario”:6,”username”:”clopez”,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_DISEÑADOR”},{“idUsuario”:7,”username”:”jvelez”,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_RADIOLOGO”},{“idUsuario”:9,”username”:”kmalagon”,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_DISEÑADOR”},{“idUsuario”:10,”username”:”cristian”,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_DISEÑADOR”},{“idUsuario”:12,”username”:”jluis”,”name”:null,”lastname”:null,”password”:”123456″,”enabled”:true,”role”:”ROLE_DISEÑADOR”}]
    UnityEngine.Debug:Log(Object)
    c__Iterator0:MoveNext() (at Assets/scripts/ParseUsingSimpleJson.cs:23)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

    but I’m stil getting the error

    NotImplementedException: null
    SimpleJSON.JSONNode.Numberize (System.String token) (at Assets/Plugins/SimpleJSON.cs:315)
    SimpleJSON.JSONNode.AddElement (SimpleJSON.JSONNode ctx, System.String token, System.String tokenName, Boolean tokenIsString) (at Assets/Plugins/SimpleJSON.cs:329)
    SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/Plugins/SimpleJSON.cs:441)
    SimpleJSON.JSON.Parse (System.String aJSON) (at Assets/Plugins/SimpleJSON.cs:1303)
    ParseUsingSimpleJson+c__Iterator0.MoveNext () (at Assets/scripts/ParseUsingSimpleJson.cs:24)
    UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

    I used this code

    using UnityEngine;
    using System.Collections;
    using SimpleJSON;

    public class ParseUsingSimpleJson : MonoBehaviour
    {
    string jsonData;

    IEnumerator Start()
    {

    string url = “https://navarcito.cfapps.io/user.json”;
    WWW www = new WWW(url);
    yield return www;

    if (www.error == null)
    {

    jsonData = http://www.text;
    }
    Debug.Log(“jsonData”+jsonData);
    JSONNode jsonNode = SimpleJSON.JSON.Parse(jsonData);

    Debug.Log(“Data ” + jsonNode[0].ToString());

    }
    }
    So I think the error it’s when I do the “Parse” with SimpleJSON

    Reply
  • February 17, 2017 at 1:20 am
    Permalink

    Ey I just figure out what was wrong, it was my simpleJSON file, I change it and everything it’s ok. Thank You for your post 🙂

    Reply
  • March 3, 2018 at 12:54 pm
    Permalink

    i need its vise versa.
    means i fetched data from server using this code.
    but i want to post data to server.

    Reply
  • May 18, 2018 at 10:19 pm
    Permalink

    Hi

    i am looking for help to print out the name of the variable. in your code you print the value and that help me but i also need to print the name of the variable. below is a piece of your code.

    // here instead of “Department”, you can also write jsonNode[0][0]
    Debug.Log (“Department 0 “+ jsonNode[“Department”][0].ToString());

    how can i refrence “jsonNode[0]” and get “Department” to print???
    in my case i will not not that 0 is Department so i need to read that info to but how?

    Reply
  • January 18, 2019 at 12:32 pm
    Permalink

    So the que is how can I parse json data to Unity’s Video Player component, it has URL field and I want to load the url from json….a little help would be great

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *