【问题标题】:ArgumentException: JSON parse error: Missing a name for object member, unity 2019.2.4f1ArgumentException:JSON 解析错误:缺少对象成员的名称,unity 2019.2.4f1
【发布时间】:2020-03-11 12:46:29
【问题描述】:

我正在尝试从 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 枚举器,System.IntPtr 返回值地址)(在 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);
            }
        }
    }

}

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;
}

播放器.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

【问题讨论】:

  • 对,你有 id、name、mobile 和 password.. 你声明了 id、title 和 body...
  • 你是对的,但我只是在获取 id,即使在声明了相同的变量后它也不起作用
  • 您是否尝试过改用JsonUtility.FromJson&lt;List&lt;Player&gt;&gt;(jsonResult)?您没有收到带有列表的对象,而是直接获得了一个列表(除非您没有粘贴整个响应)。
  • 它也不起作用
  • 玩家类需要与 Json 相同,即使您只是想获取玩家 id,jsonUtility 仍然需要解析整个 json 以获取它。

标签: c# json unity3d


【解决方案1】:

说实话,JsonUtility 一直让我头疼。我建议改用Json.NET

另外,@Parsa_Rt 指出,你的 Json 是直接引用数组,所以你可以简单地这样提取玩家:

 List<Player> playerlist = JsonConvert.DeserializeObject<List<Player>>(jsonResult);
 foreach (var player in playerlist)
 {
     print("Player Id = " + player.id);
 }

【讨论】:

    【解决方案2】:

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

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2019-01-24
      相关资源
      最近更新 更多