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);