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?