我试图在团结中读一个json时遇到了问题。我收到此消息“ArgumentException:JSON解析错误:无效的值。”。我的JSON看起来像:
[{"equipo":"Boca","pj":"5","pg":"3","pp":"2"}] and it is ok in unity.
这是我的信息类
[Serializable]
public class Response
{
public string equipo;
public int pj;
public int pg;
public int pp;
}
这是我在唤醒功能中调用的协程
IEnumerator RequestCorutine()
{
var req = new WWW("http://localhost/test/item.php?team=Boca");
yield return req;
print(req.text);
//var json = req.text;
string json = System.Text.Encoding.UTF8.GetString(req.bytes, 3,
req.bytes.Length - 3);
Response response = JsonUtility.FromJson<Response>(json);
print("Equipo: " + response.equipo + " PJ: " + response.pj + " PG: " +
response.pg + " PP: " + response.pp);
}
我在此行中收到错误:Response response = JsonUtility.FromJson<Response>(json);
我为我的英语道歉,不是那么好。感谢。
答案 0 :(得分:2)
[{"equipo":"Boca","pj":"5","pg":"3","pp":"2"}]
应该是
{"equipo":"Boca","pj":5,"pg":3,"pp":2}
因为方括号用于在json中声明数组。您声明的是List<Response>
而不是单Response
。另外,我删除了数字周围的引号。