1
\$\begingroup\$

I am working on converting a string into json format and sending the json formatted data to the server.Everything is working fine. I tested the project and exported the project.Current version of Unity I am using is Unity 5.3.5.

the problem is that when I imported this project package in Unity 4.7. Iam getting an error

"The name "JsonUtility" does not exist in the current context"

How can I resolve this issue by replacing with other function so as that get worked with Unity 4.7

Here is piece of code is used for converting into json format

  using UnityEngine;
  using System.Collections;
  using LitJson;
  //using SimpleJSON;

  using System.IO;
  public class JsonScript : MonoBehaviour {
  JsonData json;
  void Start()
   {
    Data1 data = new Data1();
    data.command = "state";
    data.payload = new Payload1() { text = "wwwwwww", image = "hello" };
    string json = JsonUtility.ToJson(data,true);
    P(json + "\t\n");

    }

// Use this for initialization
void P(string aText)
{
    print (aText +"\n");
}

 }

 [System.Serializable]
 public class Payload1
 {
    public string text;
    public string image;

   }

   [System.Serializable]
    public class Data1
     {
      public string command;
     public Payload1 payload;
     }
\$\endgroup\$
1
  • \$\begingroup\$ its JsonUtility.But its giving an error in unity 4.7 version \$\endgroup\$ Sep 30, 2016 at 7:52

1 Answer 1

1
\$\begingroup\$

According to the changelog of the documentation, the class JsonUtility was added in Unity Version 5.3.6. You said you used it in 5.3.5, so this seems to be incorrect. The earliest mentioning of it I could find is the release announcement for 5.3 which talks about "A new JSON API, that supports conversion of objects to and from JSON format".

So apparently the class JsonUtility requires at least Unity 5.3.

If you need JSON encoding and decoding in an earlier version of Unity, you will have to use a 3rd party library (or write your own).

\$\endgroup\$
4
  • \$\begingroup\$ I am trying to use SimpleJson. Is this supported with unity 4.7 \$\endgroup\$ Sep 30, 2016 at 8:50
  • \$\begingroup\$ @user1509674 A google search for "SimpleJson" returns many different tools for many different technologies. Please be more specific which one you mean. \$\endgroup\$
    – Philipp
    Sep 30, 2016 at 8:56
  • \$\begingroup\$ wiki.unity3d.com/index.php/SimpleJSON#SimpleJSON.cs. I have gone through this link \$\endgroup\$ Sep 30, 2016 at 9:39
  • \$\begingroup\$ @user1509674 The file is from 2014. Unity 4.7 was released in 2015. So it should work. When it doesn't, please open a new question. \$\endgroup\$
    – Philipp
    Sep 30, 2016 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .