【问题标题】:how to output data using JSON如何使用 JSON 输出数据
【发布时间】:2016-07-01 09:13:46
【问题描述】:

如何解决统一 3d 中的错误

Json 异常: 输入的评估不正确 JSON 文本
LitJson.JsonReader.Read()

我要做一个问答游戏,并在一个按钮中输出问题和单词 如何解决这个问题,这样游戏才能运行????请帮忙谢谢

这里是需要输入的数据

{
    "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"]
    },

这里是 c# 代码

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");
    }
}
}

【问题讨论】:

  • 根据该站点validator,您的 JSON 无效。
  • 删除最后一个逗号并在文件末尾添加] },它将修复

标签: c# json unity3d


【解决方案1】:

创建合适的类并使用JsonUtility,简单但非常有效。

【讨论】:

    【解决方案2】:

    正如 cmets 中所说,您的 json 文本无效。应该是这样的:

    {
        "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"]
        }]
    }
    

    您还可以使用查看器或验证器(例如 this)来检查 json 是否有效,知道错误在哪里,或者只是查看它的结构。

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 2021-09-20
      • 1970-01-01
      • 2017-06-05
      • 2021-11-29
      • 2019-02-21
      • 1970-01-01
      • 2019-04-10
      • 2013-11-17
      相关资源
      最近更新 更多