jess ruiz
jess ruiz

Reputation: 31

how to output data using JSON

HOW TO RESOLVE the error in unity 3d

Json Exception: Input doesn't evaluate to proper JSON text
LitJson.JsonReader.Read ()

I am going to make a quiz game and output questions and words in a button how to resolve this issue so the game will run???? please help thanks

here are the data need to be input

{
    "data": [
    {
        "id":"1",
        "que": "9 X 7",
        "ans":[ "36","54","63","81"]
    },
    {
        "id":"2",
        "que": "50 + 20",
        "ans":["60","70","90","80"]
    },
    {
    "id":"3",
    "que": "100 / 5",
    "ans":["100","50","20","500"]
    },
    {
        "id":"4",
        "que": "335 - 125",
        "ans":["200","215","220","210"]
    },
    {
        "id":"5",
        "que": "19 x 70",
        "ans":["1230","1330","1350","1340"]
    },
    {
        "id":"6",
        "que": "160 + 70",
        "ans":["214","240","220","230"]
    },
    {
        "id":"7",
        "que": "2260 / 113",
        "ans":[ "30","40","20","12"]
    },
    {
        "id":"8",
        "que": "7850 - 1487 + 350",
        "ans":["6733","6712","6713","6723"]
    },
    {
        "id":"9",
        "que": "8 X 6 / 4",
        "ans":["14","12","16","13"]
    },
    {
        "id":"10",
        "que": "70 + 30 - 15",
        "ans":["95","75","85","105"]
    },

here are the c# codes

using UnityEngine;
using System.Collections;
using LitJson;
using UnityEngine.UI;

public class Que : MonoBehaviour
{

public string filePath;
public string jsonString;
public JsonData queData;
public int numberQue=0;
public GameObject ansPrefab;

public void QueBegin()
{

    filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "Math.json");
    StartCoroutine ("Json");
    queData = JsonMapper.ToObject(jsonString);
}

IEnumerator Json()
{
    if (filePath.Contains("://"))
    {
        WWW www = new WWW(filePath);
        yield return www;
        jsonString = www.text;
    }
    else
    {
        jsonString = System.IO.File.ReadAllText(filePath);
    }

}

public void OnClick()
{
    QueBegin ();
       GameObject.Find("Que/Background/Panel/QueC/Que").GetComponentInChildren<Text>         ().text = queData["data"][numberQue]["que"].ToString();

    for (int i=0; i<queData["data"][numberQue]["ans"].Count; i++) {

        GameObject ans = Instantiate(ansPrefab);
        ans.GetComponentInChildren<Text>().text = queData["data"][numberQue]        ["ans"][i].ToString();
        Transform AnsC = GameObject.Find("AnsC").GetComponent<Transform>();
        ans.transform.SetParent(AnsC);
        if (i == 0)
        {
            ans.GetComponent<Button>().onClick.AddListener(() => Ans(1));
        }else
        {
            ans.GetComponent<Button>().onClick.AddListener(() => Ans(0));
        }
    }

    numberQue++;
}
public void Ans(int x)
{
    if (x == 1)
    {
        Debug.Log("Answer Correct");
    }
    else
    {
        Debug.Log("Answer Wrong");
    }
}
}

Upvotes: 0

Views: 330

Answers (2)

Jo&#227;o Martins
Jo&#227;o Martins

Reputation: 332

As said in the comments, your json text is not valid. Should be like this:

{
    "data": [{
        "id": "1",
        "que": "9 X 7",
        "ans": ["36", "54", "63", "81"]
    }, {
        "id": "2",
        "que": "50 + 20",
        "ans": ["60", "70", "90", "80"]
    }, {
        "id": "3",
        "que": "100 / 5",
        "ans": ["100", "50", "20", "500"]
    }, {
        "id": "4",
        "que": "335 - 125",
        "ans": ["200", "215", "220", "210"]
    }, {
        "id": "5",
        "que": "19 x 70",
        "ans": ["1230", "1330", "1350", "1340"]
    }, {
        "id": "6",
        "que": "160 + 70",
        "ans": ["214", "240", "220", "230"]
    }, {
        "id": "7",
        "que": "2260 / 113",
        "ans": ["30", "40", "20", "12"]
    }, {
        "id": "8",
        "que": "7850 - 1487 + 350",
        "ans": ["6733", "6712", "6713", "6723"]
    }, {
        "id": "9",
        "que": "8 X 6 / 4",
        "ans": ["14", "12", "16", "13"]
    }, {
        "id": "10",
        "que": "70 + 30 - 15",
        "ans": ["95", "75", "85", "105"]
    }]
}

You can also use viewers or validators like this to check if the json is valid, know where's the error or to simply view it's structure.

Upvotes: 1

Adrian Famulski
Adrian Famulski

Reputation: 31

create appropriate class and use JsonUtility, is simple but very efective.

Upvotes: 1

Related Questions