首页 > 解决方案 > 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

标签: c#jsonunity3d

解决方案


在您的回复中,您已经声明了一个列表,但该列表没有任何名称,您如何拥有一个没有名称的 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"
       }
     ]

}


推荐阅读