0

I'm trying to create a list of notifications in Unity, which are delivered by a JSON API. I'm using the SimpleJson plugin, which I already used in another scene perfectly,

Here is the JSON:

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

Here is the error:

Exception: JSON Parse: Quotation marks seems to be messed up. SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/QRcode/Scripts/SimpleJSON.cs:735) 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)

I'm using this code to call JSON:

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

public class DataLoader : MonoBehaviour
{
    string JsonDataString;
    string JsonDataString2;
    static public string OriginalJsonSite = "http://(web service name)/API/testnot.php"; 

    public Text TituloNot;
    public Text TextoNot;

    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);
        JSONArray array = jsonNode.AsArray;
        Debug.Log(JsonDataString2);

        TituloNot.text = array[0]["titulo"].ToString();
        Debug.Log(jsonNode["titulo"]);
        TextoNot.text = array[0]["texto"].ToString();
        Debug.Log(jsonNode["texto"]);
    }
}
  • 3
    Why are you taking a substring of the returned json before parsing it? – Jonathon Chase Jul 22 '19 at 17:39
  • To eliminate the eighth notes of the Json [] (Sorry my bad English, I do not know how to say it). Also, if I delete them, the error becomes a NullReferenceException. – Eduardo Herrera Jul 22 '19 at 17:43
  • 1
    does the JSON come exactly in the format you posted or in one line? Best would be you set Breakpoints and investigate the value of `JsonDataString2` ... I guess you are either cutting the first or the last `"` of when doing `Substring` – derHugo Jul 22 '19 at 18:27
  • In one line, it was recently published like this. – Eduardo Herrera Jul 22 '19 at 18:30
  • I reduced the values to 1 and 2 in the substring, and now it throws me an error of NullReferenceException: Object reference not set to an instance of an object DataLoaderNot + d__5.MoveNext () (at Assets / QRcode / Scripts / DataLoaderNot.cs: 33), which corresponds to the line TituloNot.text = array [0] ["titulo"]. ToString (); – Eduardo Herrera Jul 22 '19 at 18:39
  • Start removing properties from your call, until you have the most stripped-down version that still causes errors. Any one of those 3 properties could be null. – Eliasar Jul 22 '19 at 21:06
  • 1
    I'm wondering what's wrong with [Unity's JsonUtility](https://docs.unity3d.com/ScriptReference/JsonUtility.html) – Lyrca Jul 22 '19 at 22:25
  • Possible duplicate of [Serialize and Deserialize Json and Json Array in Unity](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity) – derHugo Jul 23 '19 at 07:30
  • BTW, `[]` are called "square brackets" (with `{}` being "curly braces" and `<>` being "angle brackets", because they are "square" "squiggly" and "angular" respectively). `♪` is an eighth note. – Draco18s no longer trusts SE Jul 23 '19 at 13:51

1 Answers1

0

The lines

Debug.Log(jsonNode["titulo"]);
Debug.Log(jsonNode["texto"]);

won't work for sure, you should remove them.

Also, if the returned JSON is really exactly like the one you posted, you should also remove the line

JsonDataString2 = JsonDataString.Substring(3, JsonDataString.Length - 4);

The parse will take care of finding matching brackets [] and braces {} so this line will break it.

Edit: I didn't test it but this code should work in my opinion:

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

public class DataLoader : MonoBehaviour
{
    string jsonDataString;

    static public string originalJsonSite = "http://(web service name)/API/testnot.php";

    public Text tituloNot;
    public Text textoNot;

    IEnumerator Start()
    {
        WWW readingsite = new WWW (originalJsonSite);
        yield return readingsite;
        if (string.IsNullOrEmpty(readingsite.error))
        {
            jsonDataString = readingsite.text;
        }
        JSONNode jsonNode = SimpleJSON.JSON.Parse(jsonDataString);
        JSONArray array = jsonNode.AsArray;
        tituloNot.text = array[0]["titulo"].ToString();
        textoNot.text = array[0]["texto"].ToString();
    }
}
Linus
  • 111
  • 4
  • No, if I remove that line, the JSON does not read well, due to the []. And I get the 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) – Eduardo Herrera Jul 22 '19 at 23:15
  • 1
    The [] are required, they tell the parser that it's an array! Did you also replace JsonDataString2 by JsonDataString? JsonDataString2 is null without the substring line so of course there is a NullReferenceException. – Linus Jul 23 '19 at 08:05