3

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.