Reputation: 502
I'm trying to fetch data from php api's. stuck on this error
ArgumentException: JSON parse error: Missing a name for object member. 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 title;
public string body;
}
It's my complete code for fetching data from web api's, but getting
ArgumentException: JSON parse error: Missing a name for object member
I'm using unity 2019.2.4f1
Upvotes: 1
Views: 6574
Reputation: 3576
To be honest, JsonUtility
has always given me nothing but headaches. I recommend using Json.NET instead.
In addition, @Parsa_Rt has pointed out, your Json is directly referencing the array, so you could simply extract the players this way:
List<Player> playerlist = JsonConvert.DeserializeObject<List<Player>>(jsonResult);
foreach (var player in playerlist)
{
print("Player Id = " + player.id);
}
Upvotes: 0
Reputation: 73
in your response you have declared a list but the list does not have any name , how can you have a List variable without name , all variables must have names. in your PlayerList class you have a variable called "players" holding a list of players , but in your response you didnt declare that so in order for it to work your response must look like this :
{
"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"
}
]
}
Upvotes: 2