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"
}
5
  • 2
    There’s the json.net library Commented Jun 16, 2022 at 1:08
  • 1
    Does this answer your question? How to write JSON string value in code? Commented Jun 16, 2022 at 1:14
  • @DanielA.White How do I import it to Unity? Commented Jun 16, 2022 at 1:45
  • @Jimmar Not really, sorry. I want to create a JSON in another way than how i did Commented Jun 16, 2022 at 1:45
  • 1
    A quick google showed that there's Json.NET for unity on unity asset store. You might want to check that out. Commented Jun 16, 2022 at 2:34

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.