如何在团结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");
}
}
}
答案 0 :(得分:1)
创建合适的类并使用JsonUtility,虽然简单但非常有效。
答案 1 :(得分:1)
如评论中所述,您的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是否有效,知道错误的位置或只是查看它的结构。