-2

I'm struggling with extracting a parameter of a JsonObject I retrieve from my server.

public class DAO : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(GetRequest("http://localhost:5001/dangermonsters/us-central1/ping"));
    }

    private IEnumerator GetRequest(string uri)
    {
        UnityWebRequest uwr = UnityWebRequest.Get(uri);

        yield return uwr.SendWebRequest();

        if(uwr.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("Error while sending" + uwr.error);
        } else
        {
            Debug.Log(uwr.downloadHandler.text);   
        }
    }
}

This is what it's printed

{"status":true}

How can I retrieve the "status" parameter inside the object received from the server?

Evorlor
  • 7,263
  • 17
  • 70
  • 141
Allen
  • 19
  • 4

1 Answers1

0

I found out the solution. The code to do this is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json.Linq;
using UnityEngine.Networking;


public class DAO : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(GetRequest("http://localhost:5001/dangermonsters/us-central1/ping"));
    }

    private IEnumerator GetRequest(string uri)
    {
        UnityWebRequest uwr = UnityWebRequest.Get(uri);

        yield return uwr.SendWebRequest();

        if(uwr.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log("Error while sending" + uwr.error);
        } else
        {
            JObject obj = JObject.Parse(uwr.downloadHandler.text);
            Debug.Log(obj["status"]);
        }
    }
}

Allen
  • 19
  • 4
  • Well that's one way .. see the duplicate link for more Unity specific solutions ;) In particular Unity already has a built-in JSON serializer – derHugo Feb 19 '22 at 21:18