I need to send some data as JSON to a server, and using JSONUtility didn't really work as well as I thought, is there a simple and more readable way to write this?
public void ObtenerDatos()
{
if(UrlInput.text != "")
{
DatosJson = string.Format("{{" +
"\"topic\":\"{0}\"," +
"\"title\":\"{1}\"," +
"\"message\":\"{2}\"," +
"\"url\":\"{3}\"," +
"\"actions\":[{{" +
"\"action\":\"view\"," +
"\"label\":\"{4}\"," +
"\"url\":\"{3}\"" +
"}}]" +
"}}",TopicoInput.text,TituloInput.text,MensajeInput.text,UrlInput.text,AccionInput.text);
}
else
{
DatosJson = string.Format("{{" +
"\"topic\":\"{0}\"," +
"\"title\":\"{1}\"," +
"\"message\":\"{2}\"" +
"}}", TopicoInput.text, TituloInput.text, MensajeInput.text);
}
StartCoroutine(EnviarMensaje(DatosJson));
}
The JSON should look like this if the URL is provided:
{
"topic": "topic",
"title": "title",
"message": "message",
"click": "url",
"actions": [
{
"action": "view",
"label": "label",
"url": "url"
}
]
}
And like this, if no URL is provided:
{
"topic": "topic",
"title": "title",
"message": "message"
}