0

I'm trying to download json from the web and parse it then get the data and store it, however when i get the json from the server, i can't parse it as it's returning null for some values although it does have a value, and when i try to get specific data it returns actual null error. Now i know this question has been asked a lot of times. and i did search and tried many solutions however nothing seemed to work for me... I have used json to cshap to generate my json class in order to store the data in jsoncsharp Link

Here's the json that i download

{
"status": "success",
"result": {
"slug": "test",
"id": "26",
"timer_1": "15",
"timer_2": "10",
"pass_1": "aaaa",
"pass_2": "bbbb",
"count": "4",
"port": "66",
"network": "*******************",
"network2": "******************",
"network_api_consumer_key": "******************",
"network_api_consumer_secret": "******************",
"network_api_user_access_token": "******************",
"network_api_user_access_secret": "******************"
 }
 }

and here's the class where i store the values. this is generated by jsoncsharp

[System.Serializable]
public class Result
{
  public string slug { get; set; }
  public string id { get; set; }
  public string timer_1 { get; set; }
  public string timer_2 { get; set; }
  public string pass_1 { get; set; }
  public string pass_2 { get; set; }
  public string count { get; set; }
  public string port { get; set; }
  public string network { get; set; }
  public string network2 { get; set; }
  public string network_api_consumer_key { get; set; }
  public string network_api_consumer_secret { get; set; }
  public string network_api_user_access_token { get; set; }
  public string network_api_user_access_secret { get; set; }
}

[System.Serializable]
public class root
{
  public string status { get; set; }
  public Result result { get; set; }
}

and this is my method when i download

 IEnumerator Fetch(string url)
{
    UnityWebRequest www = UnityWebRequest.Get(url);
    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error);
    }
    else
    {
        // Show results as text
        Debug.Log(www.downloadHandler.text);
        string jsonString = www.downloadHandler.text;
        root data = JsonUtility.FromJson<root>(jsonString);
        Debug.Log(data.status); //returns text null
        Debug.Log(data.result.port); // returns actual null error 

    }
}

Thank you.

David
  • 361
  • 2
  • 3
  • 16
  • Can you specify which values becomes null after parsing? Your JSON also seems to broken in the last 4 `network_api_...` values. – Johan Oct 06 '18 at 10:18
  • at the end of the question in my method i add a comment beside my Debug. when i debug what i download from the server i get the whole json with the data, however when i try to parse it i get null data, for example data.status actual returns null and data.result.port gives null error – David Oct 06 '18 at 10:20
  • 1
    sorry for that, it's fixed now – David Oct 06 '18 at 10:21

1 Answers1

2

JsonUtility does not support properties. You need to remove all the

{ get;set; }
Everts
  • 10,408
  • 2
  • 34
  • 45
  • I will try that and come back to you – David Oct 06 '18 at 10:32
  • 1
    lol.... it's fixed everything is working fine now.. omg..... i have been stuck for a full day for this.. this is my first time i use Unity json and probably my last... – David Oct 06 '18 at 10:33