简体   繁体   中英

Unity json data from server returns null

I have a problem, where Json data is taken from server and then called out, but always outputs NULL. You can test code if you want to, Url is public.

This is Json data that I get from server
[{"id":"3","question":"Cik ir 4 + 4 = ?","answer1":"3","answer2":"13","answer3":"7","answer4c":"8"}]
How do I fix this, so Json data outputs correctly?

Thank you.

using System.Collections;
using System.Collections.Generic;
using System.Net.Mime;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;

public class Code : MonoBehaviour
{
    public Text first;
    public const string URLBase = "http://91.200.67.104:8080/test/json.php";

    [System.Serializable]
    public class Results
    {
        public string id;
        public string question;
        public string answer1;
        public string answer2;
        public string answer3;
        public string answer4c;
    }

    void Start()
    {
        StartCoroutine(Run());
    }

    IEnumerator Run()
    {
        var req = CreateReturnPlayerDataRequest();
        yield return req.SendWebRequest();
        var results = HandleReturnPlayerDataRequest(req);
        first.text = results.id + "  dasdasdas  " + results.question;
    }

    public static UnityWebRequest CreateReturnPlayerDataRequest()
    {
        var req = UnityWebRequest.Get(URLBase);
        return req;
    }
    public static Results HandleReturnPlayerDataRequest(UnityWebRequest req)
    {
        if (req.isNetworkError)
        {
            Debug.LogError("Failed to POST /player/register");
            return new Results();
        }
        // Debug izvada datus console
        Debug.Log("{\"Results\":" + req.downloadHandler.text + "}");
        Results results = JsonUtility.FromJson<Results>("{\"Results\":" + req.downloadHandler.text + "}");
        Debug.Log("ID:" + results.id + "  ;question: " + results.question + "   ; " + results.answer1);
        Debug.Log(results.id);
        Debug.Log(results.question);
        Debug.Log(results.answer1);
        return results;
    }
}

Your deserialization class structure is incorrect.

Firstly, the json data you are receiving from the server is an array/collection (it is surrounded by square brackets "[ ]").

Secondly, you are placing it in the "Results" field of a parent object.

Therefore, this would be the correct structure:

[System.Serializable]
public class RootObject
{
    public List<Results> Results;
}

[System.Serializable]
public class Results
{
    public string id;
    public string question;
    public string answer1;
    public string answer2;
    public string answer3;
    public string answer4c;
}

Usage:

var obj = JsonUtility.FromJson<RootObject>("{\"Results\":" + req.downloadHandler.text + "}");
Results serverData = obj.Results[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM