webRequest.downloadHandler.text. Exception in JsonUtility in Unity

Here my classes

[Serializable]
public class ApiResponse
{
[SerializeField]
public List data;
}

[Serializable]
public class ApiLobby
{
[SerializeField]
public string lobbykey;
}
I get json from server:

{“data”:[{“id”:“4”,“datetime”:“2019-01-26 08:01:38”,“creatorid”:“24”,“private”:“0”,“lobbykey”:“0c89dfa7”,“state”:“CREATED”,“type”:“FIELD”,“sizex”:“30”,“sizey”:“20”,“imageid”:“1”}]}

But when i try create ApiResponse from it by JsonUtility call:

Network.ApiResponse<Network.ApiLobby> apiResponse = JsonUtility.FromJson<Network.ApiResponse<Network.ApiLobby>>(jsonFromText);
I got exception:

ArgumentException: JSON parse error: Invalid value.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:50)
UnityEngine.JsonUtility.FromJson[ApiResponse`1] (System.String json) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:33)
TestingControllerScript.EasySoftware.NetworkScript.INetworkListener.NetworkResponseSuccess (System.String action, UnityEngine.Networking.UnityWebRequest webRequest) (at Assets/Scripts/Testing/TestingControllerScript.cs:45)
EasySoftware.NetworkScript+c__Iterator0.MoveNext () (at Assets/Scripts/Common/NetworkScript.cs:55)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
But the strange is then i just paste thus json in editor and try parse again - all work fine!!!

string jsonInEditor = “{"data":[{"id":"4","datetime":"2019-01-26 08:01:38","creatorid":"24","private":"0","lobbykey":"0c89dfa7","state":"CREATED","type":"FIELD","sizex":"30","sizey":"20","imageid":"1"}]}”;//
Network.ApiResponse<Network.ApiLobby> apiResponse = JsonUtility.FromJson<Network.ApiResponse<Network.ApiLobby>>(jsonInEditor);
Why? And how to fix it?

I also try already convert response like:

string jsonFromBytes = System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data, 3, webRequest.downloadHandler.data.Length - 3);
No effect, same exception.

You are the only person I can find that is having the same issue with pulling a Json file from a UnityWebRequest and having invalid value. If you have figured it out I would appreciate any info. Im pulling my hair out since the documentation of the actual JsonUtil is utter garbage and doesnt have error handling.

I didn’t go through your json line by line, but I’m going to assume everything is formatted properly. I had this issue with even a simple Json file:
"{ “anInt” : 5 } "

The issue was the same as appears here with WWW: JSONUtility.FromJson Error: Invalid Value

My JSON file was encoded as UTF-8 with BOM (Byte order mark). I created it in VSCode and it seems that it’s Microsoft best practice to include the BOM.

Maybe someone knows the best way to determine the text format, but if you can assume the file is UTF-8 with BOM you can strip the Byte Order Mark as shown in the post I linked.

Ex:

var jsonString = System.Text.Encoding.UTF8.GetString(
   unityWebRequest.downloadHandler.data,
   3,
   unityWebRequest.downloadHandler.data.Length - 3);