1
\$\begingroup\$

I am working on communicating unity application via a series of Json. Since I am new to JSON, I don't have any idea how that work with Wnity.

Here is my JSON structure

{
    "id": “id",
    "payload": {
            “image”: "Image Url”
            "text":"text message"
            "option" : {
               "option1":"1" ,
               "option2": "2"
                }
    }

}

Now I have created a button in Unity scene. I have hardcoded the image and text data. When I click on the button the data should get converted into Json and should send JSON data to the server and print a data log message.

I have used this code.Iam getting the output

using UnityEngine;
using System.Collections;
using LitJson;
using System.IO;
public class JsonScript : MonoBehaviour {
JsonData json;
void Start()
{
    Data data = new Data();
    data.id = 1;
    data.payload = new Payload() { text = "wwwwwww", image = "hello" };
    //json = JsonMapper.ToJson(data) ;
    string json = JsonUtility.ToJson(data);
    P (json);
    //      P(json + "\t\n");

 }

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

}

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

     [System.Serializable]
    public class Data
  {
        public int id;
        public Payload payload;
  }

The output is getting displayed in a single line

{"id":1,"payload":{"text":"wwwwwww","image":"hello"}}

But I need the output to be printed as the Json format.I tried giving space.

Can anybody please help me out?

\$\endgroup\$
3
\$\begingroup\$

https://docs.unity3d.com/Manual/JSONSerialization.html
The documentation has very detailed explaination. In your case the classes/structures (that store data ) should look like:

[Serializable]
public class Payload
{
    public string imageUrl;
    public string text;
}

[Serializable]
public class Data
{
    public int id;
    public Payload payload;
}

Mark them with Serializable attribute so that they can be serialized.
To serialize it , you do:

Data data = new Data();
data.id = <any id>;
data.payload = new Payload() { imageUrl = <any url>, text = <any text> };

string json = JsonUtility.ToJson(data);

And to convert the json string back into data you do:

var data = JsonUtility.FromJson<Data >(json);

That's it, hope this helps.


Edit 01: Pretty format JSON
(in the same documentation as above )

Controlling the output of ToJson()

ToJson supports pretty-printing the JSON output. It is off by default but you can turn it on by passing true as the second parameter.

So i guess all you need to do is just :

string json = JsonUtility.ToJson(data,true);

Honestly, I havent tried it yet but I do believe in what the documentation says :D


Edit02 :

[Serializable]
public class Option
{
    public int option01;
    public int option02;
}
[Serializable]
public class Payload
{
    public string imageUrl;
    public string text;
}

[Serializable]
public class Data
{
    public int id;
    public Payload payload;
    public Option option;
}

Data data = new Data();
data.id = <any id>;
data.payload = new Payload() { imageUrl = <any url>, text = <any text> };
data.option= new Option() { option01 = 1, option02 = 2 };

string json = JsonUtility.ToJson(data,true);
\$\endgroup\$
  • \$\begingroup\$ Thanks a lot for your help. Its working fine. I have a dout the output is getting displayed as {"id":1,"payload":{"text":"wwwwwww","image":"hello"}} in a single line .how can I display the message as the json structure as mentioned in the question \$\endgroup\$ – user1509674 Sep 27 '16 at 4:03
  • \$\begingroup\$ @user1509674 I think what you are looking for is printing a "beautified" version of the JSON, cf. stackoverflow.com/questions/6178544/how-can-i-beautify-a-json \$\endgroup\$ – Shautieh Sep 27 '16 at 6:16
  • \$\begingroup\$ @Shautieh : I went through the link you provided,but Newtonsoft.Json is a paid library right.Is there any other library.Here I have used LitJson and SimpleJSON \$\endgroup\$ – user1509674 Sep 27 '16 at 6:51
  • \$\begingroup\$ @user1509674 The second answer seems good enough: markdavidrogers.com/… . Otherwise I'm not using one so I can't recommend any, but there should be plenty just googling "c# json beautifier"! \$\endgroup\$ – Shautieh Sep 27 '16 at 8:05
  • \$\begingroup\$ @user1509674 : Hey, I edited my answer, just take a look. \$\endgroup\$ – tc07 Sep 27 '16 at 8:15

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.