1

I'm fetching web api's in unity, but getting error. here is the error.

ArgumentException: JSON must represent an object type. UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:42) UnityEngine.JsonUtility.FromJson[T] (System.String json) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:30) RestClient+d__3.MoveNext () (at Assets/_Scripts/RestClient.cs:34) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

here is my json response

[
   {
      "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 name;
    public string mobile;
    public string password;
}
Imran Iqbal
  • 478
  • 5
  • 15
  • 2
    The JSON represents an array of objects, but `JsonUtility.FromJson` apparently doesn't support deserializing an array as the root JSON value. For details + workarounds see [Unity Deserializing from server](https://stackoverflow.com/q/43231851/3744182) and [Serialize and Deserialize Json and Json Array in Unity](https://stackoverflow.com/q/36239705/3744182). In fact I think this is a duplicate of those two, agree? – dbc Nov 18 '19 at 06:05

1 Answers1

4

I guess, that JsonUtility.FromJson<T> is expecting an object not "just" an array.
So the json should look something like this:

{
    "players": [
        {
            "id":"1",
            "name":"Yasir",
            "mobile":"0301",
            "password":"123"
        },
        ...
    ]
}

and then the PlayerList´s field players must not be static.

JakobFerdinand
  • 1,087
  • 7
  • 20