Saturday, July 20, 2019

c# - Communicate Unity application via a series of Json


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?



Answer



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 = ;
data.payload = new Payload() { imageUrl = , text = };

string json = JsonUtility.ToJson(data);


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


var data = JsonUtility.FromJson(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 = ;
data.payload = new Payload() { imageUrl = , text = };
data.option= new Option() { option01 = 1, option02 = 2 };

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

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...