Reputation: 13
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"]);
}
}
Upvotes: 0
Views: 1637
Reputation: 111
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();
}
}
Upvotes: 0