2

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"
}
Jimmar
  • 4,194
  • 2
  • 28
  • 43

1 Answers1

3

This can be solved by using the JsonUtility class provided by UnityEngine namespace.

First, Create a data class to extract as JSON.

[Serializable]
public class IncludeUrlJsonContainer
{
  public string topic;
  public string title;
  public string message;
  public string click;
  public ActionsJson[] actions;

  [Serializable]
  public class ActionsJson
  {
    public string action;
    public string label;
    public string url;
  }
}
[Serializable]
public class NonIncludeUrlJsonContainer
{
  public string topic;
  public string title;
  public string message;
}

Second, Fill in the values in a method that generates JSON data. (It is filled in differently depending on the url or not.) Then, extract it to JSON using JsonUtility.ToJson().

public void CreateJsonData()
{
  string jsonData;
    
  if (urlInput.text != "")
  {
    var includeUrlJsonContainer = new IncludeUrlJsonContainer()
    {
      topic = topicInput.text,
      title = titleInput.text,
      message = messageInput.text,
      click = urlInput.text,
      actions = new[]
      {
        // If the number of arrays increases, add more
        new IncludeUrlJsonContainer.ActionsJson()
        {
          action = "view",
          label = actionInput.text,
          url = urlInput.text
        }
      }
    };
    jsonData = JsonUtility.ToJson(includeUrlJsonContainer, true);
  }
  else
  {
    var nonIncludeUrlJsonContainer = new NonIncludeUrlJsonContainer()
    {
      topic = topicInput.text,
      title = titleInput.text,
      message = messageInput.text
    };
    jsonData = JsonUtility.ToJson(nonIncludeUrlJsonContainer, true);
  }

  Debug.Log(jsonData);
}

The following is the output result by inputting a random value.

When there is a value in urlInput.text

{
    "topic": "topic",
    "title": "title",
    "message": "message",
    "click": "url",
    "actions": [
        {
            "action": "view",
            "label": "action",
            "url": "url"
        }
    ]
}

When urlInput.text is empty

{
    "topic": "topic",
    "title": "title",
    "message": "message"
}

I referred to the information you provided when writing the code, but I modified the part in Spanish.

Hope your problem is solved :)

isakgo_
  • 750
  • 4
  • 15