c# - ArgumentException:JSON 解析错误:缺少对象成员的名称,unity 2019.2.4f1
问题描述
我正在尝试从 php api 获取数据。卡在这个错误上
ArgumentException:JSON 解析错误:缺少对象成员的名称。UnityEngine.JsonUtility.FromJson (System.String json, System.Type 类型) (在 C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:42) UnityEngine.JsonUtility.FromJson[T] ( System.String json) (在 C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:30) RestClient+d__3.MoveNext () (在 Assets/_Scripts/RestClient.cs:34) UnityEngine .SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (在 C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
这是我的 json 回复
[
{
"id":"1",
"name":"Yasir",
"mobile":"0301",
"password":"123"
},
{
"id":"2",
"name":"Mehmood",
"mobile":"0302",
"password":"123"
},
{
"id":"3",
"name":"Imran",
"mobile":"0301",
"password":"123"
},
{
"id":"4",
"name":"Iqbal",
"mobile":"0302",
"password":"123"
}
]
ReastClient.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class RestClient : MonoBehaviour
{
private static RestClient _instance;
public static RestClient Instance{
get{
if(_instance == null){
_instance = FindObjectOfType<RestClient>();
if(_instance == null){
GameObject go = new GameObject();
go.name =typeof(RestClient).Name;
_instance = go.AddComponent<RestClient>();
}
}
return _instance;
}
}
public IEnumerator Get(string url, System.Action<PlayerList> callBack)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
yield return www.SendWebRequest();
if (www.isNetworkError)
{
print(www.error);
}else if (www.isDone)
{
string jsonResult = System.Text.Encoding.UTF8.GetString(www.downloadHandler.data);
PlayerList playerlist = JsonUtility.FromJson<PlayerList>(jsonResult);
callBack(playerlist);
}
}
}
}
游戏.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Game : MonoBehaviour
{
public string web_url = "";
// Start is called before the first frame update
void Start()
{
StartCoroutine(RestClient.Instance.Get(web_url,GetPlayers));
}
void GetPlayers(PlayerList playerlist)
{
foreach(Player player in PlayerList.players)
{
print("Player Id = " + player.id);
}
}
}
播放器列表.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerList
{
public static List<Player> players;
}
播放器.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Player
{
public int id;
public string title;
public string body;
}
这是我从 web api 获取数据的完整代码,但是得到
ArgumentException:JSON 解析错误:缺少对象成员的名称
我正在使用统一 2019.2.4f1
解决方案
在您的回复中,您已经声明了一个列表,但该列表没有任何名称,您如何拥有一个没有名称的 List 变量,所有变量都必须有名称。在您的 PlayerList 类中,您有一个名为“players”的变量,其中包含一个玩家列表,但在您的回复中您没有声明,因此为了使其正常工作,您的回复必须如下所示:
{
"Player":
[
{
"id":"1",
"name":"Yasir",
"mobile":"0301",
"password":"123"
},
{
"id":"2",
"name":"Mehmood",
"mobile":"0302",
"password":"123"
},
{
"id":"3",
"name":"Imran",
"mobile":"0301",
"password":"123"
},
{
"id":"4",
"name":"Iqbal",
"mobile":"0302",
"password":"123"
}
]
}
推荐阅读
- java - 如何在片段中为添加/删除节点(使用 grathstream)创建触摸事件?
- django - 为什么在我单击查看图像/查看 PDF 按钮之前,我的脆表单无法加载?
- mysql - MySQL如何进行浮点加法的数学计算?
- react-native - 如何通过向下滚动自动隐藏反应导航标题?
- spacy - 最新版本的 spacy-lefff 与最新版本的 spacy 不兼容
- python - Python程序通过sublime而不是命令行运行
- python - 尝试对字典中的键值对求和
- java - 有没有办法用 maven 签出 git 存储库到某个文件夹?
- javascript - 这段代码不起作用&他们是不是使用 else if 语句更有效的方式来做到这一点?
- c - 可变大小的对象可能未初始化