Answer the question
In order to leave comments, you need to log in
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);
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)
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)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question