1

I'm trying to create a list of notifications in Unity, which are delivered by a JSON API. The idea is to make a list of news, announcements, etc., where it shows the title and content of the advertisement. In this case it would be "titulo" and "texto". The url is "(web service name)/API/testnot.php"

This is what JSON gives me:

[{"_id":{"$oid":"5d30eccda6e0712cfd0832c3"},"titulo":"Primera Notificacion","texto":"Prueba de notificacion"},{"_id":{"$oid":"5d336c36a6e07114ac728cc2"},"titulo":"Segunda notificacion","texto":"Prueba de notificacion 2"}]

I'm using this code to call JSON:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using SimpleJSON;

public class DataLoaderNot : MonoBehaviour
{
    string JsonDataString;
    string JsonDataString2;
    static public string OriginalJsonSite = "http://(nombre de sitio)/API/testnot.php"; 

    public Text Titulo;
    public Text Texto;

    IEnumerator Start ()
    {
        WWW readingsite = new WWW (OriginalJsonSite);
        Debug.Log(OriginalJsonSite);
        yield return readingsite;


        if (string.IsNullOrEmpty (readingsite.error)) {
            JsonDataString = readingsite.text;
            JsonDataString2 = JsonDataString.Substring(3, JsonDataString.Length - 4);
        }


        JSONNode jsonNode = SimpleJSON.JSON.Parse (JsonDataString2);
        Debug.Log(JsonDataString2);

        Titulo.text = jsonNode["titulo"].ToString().ToUpper();
        Debug.Log(jsonNode["titulo"]);
        Texto.text = jsonNode["texto"].ToString().ToUpper();
        Debug.Log(jsonNode["texto"]);
    }
}

And Unity gives me this error:

NullReferenceException: Object reference not set to an instance of an object SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/QRcode/Scripts/SimpleJSON.cs:587) SimpleJSON.JSON.Parse (System.String aJSON) (at Assets/QRcode/Scripts/SimpleJSON.cs:1421) DataLoaderNot+d__5.MoveNext () (at Assets/QRcode/Scripts/DataLoaderNot.cs:29) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

Slanx
  • 11
  • 2

0 Answers0