Hey guys,
I have a really weird problem. I'm trying to get some data from a REST service. I'm using the following code:
private void GetTheScores()
{
UnityWebRequest GetCommand = UnityWebRequest.Get(url);
UnityWebRequestAsyncOperation operation = GetCommand.SendWebRequest();
if (!operation.webRequest.isNetworkError)
{
ResultsContainer rez = JsonUtility.FromJson<ResultsContainer>(operation.webRequest.downloadHandler.text);
Debug.Log("Text: " + operation.webRequest.downloadHandler.text);
}
}
The problem is that when I'm in Unity's editor, the request doesn't return anything (operation.webRequest.downloadHandler.text is empty, the Debug.Log command just prints "Text: "), but when I enter the debug mode and insert a breakpoint on that line, then it returns the text properly. Does anyone have an idea why is this happening?
The real problem I'm trying to solve is that when I receive the text, I can't get the data from the JSON. The markup is really simple:
[{"id":1,"name":"Player1"},{"id":2,"name":"Player2"}]
and I have an object that should accept that data:
[System.Serializable]
public class ResultScript {
public int id;
public string name;
}
There is also a class that should accept the array of these objects (which the JSON is returning):
[System.Serializable]
public class ResultsContainer {
public ResultScript[] results;
}
But when I run the code (in the debug mode, to get any result) I get an error: ArgumentException: JSON must represent an object type. I've googled it but none of the proposed solutions work for me.
Also (regardless if I'm in the debug mode or not) when I try to do some string operations like removing or adding characters to the GET result, the functions return an empty string as a result
Can you help me with any of these problems?
Thank you