我试图从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](位于C:/BuildSlave/Unity/Build/Modules/JsonSerialize/Public/JsonUtility.Bindings.cs:30)RestClient+D_3.MoveNext()
下面是我的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);
}
}
}
}
Game.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);
}
}
}
PlayerList.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerList
{
public static List<Player> players;
}
Player.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分析错误:缺少对象成员的名称
我正在使用unity 2019.2.4F1
在您的响应中您声明了一个列表但是列表没有任何名称,您怎么可能有一个没有名称的列表变量,所有的变量都必须有名称。在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"
}
]
}
老实说,JsonUtility
总是让我头疼。我建议改用JSON.NET。
此外,@parsa_rt已经指出,您的Json直接引用了数组,因此您可以简单地通过以下方式提取播放器:
List<Player> playerlist = JsonConvert.DeserializeObject<List<Player>>(jsonResult);
foreach (var player in playerlist)
{
print("Player Id = " + player.id);
}