A
A
Alexander2020-10-24 15:42:13
JSON
Alexander, 2020-10-24 15:42:13

How to process received JSON in unity - knocks out an error?

Guys, help out the stupid, I ran into a problem that when I send a post request to the server, the server generates a key and sends it to JSON, however, no matter how much I googled, I get an error.
I launch a coroutine

PostStruct user = new PostStruct()
        {
            Login = "test",
            Password = "test",
            key = "TestKey" // исходящий ключ по дефолту в принципе пустой
        };
 
        string json = JsonUtility.ToJson(user);
 
        //Debug.Log(json);
 
        UnityWebRequest request = UnityWebRequest.Post("https://" + serverURI + "/login.php", json);
 
        byte[] postBytes = Encoding.UTF8.GetBytes(json);
 
        UploadHandler uploadHandler = new UploadHandlerRaw(postBytes);
 
        request.uploadHandler = uploadHandler;
 
        request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");
 
        yield return request.SendWebRequest();
 
        Debug.Log(request.downloadHandler.text);
 
        PostStruct postStructFromServer = JsonUtility.FromJson<PostStruct>(request.downloadHandler.text);


It seems to be sent and sent, but on the last line, where the parsing is going on, it knocks out
the Error Code
ArgumentException: JSON parse error: Invalid value.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <bd9566cca22541e58ad28d1fa2849830>:0)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <bd9566cca22541e58ad28d1fa2849830>:0)
AuthRequest+<SendRequest>d__3.MoveNext () (at Assets/Auth/Scripts/AuthRequest.cs:58)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <04258d1cdc1044248c2a17a6a31a3cf7>:0)

But in the debug log it shows that the answer came, that it does not parse, please help

Array
(
    [Login] => test
    [Password] => test
    [key] => TestKey
)
{"Login":"test","Password":"test","key":"kKCsDu836n"}
UnityEngine.Debug:Log(Object)
<SendRequest>d__3:MoveNext() (at Assets/Auth/Scripts/AuthRequest.cs:56)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

ps tried to google, played with the structure

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question