Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Loading Json File with Arrays from Server

Discussion in 'Scripting' started by VincentBlaser, Jan 23, 2018.

  1. VincentBlaser

    VincentBlaser

    Joined:
    Jan 23, 2018
    Posts:
    3
    So i got an Json File like:
    Code (CSharp):
    1. {
    2.     "name": "Project 1",
    3.     "views": [
    4.         { "href": "YourURL" }
    5.     ]
    6. }
    My own classes looks like this
    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class JsonClass  {
    4.     public string name;
    5.     public views[] views;
    6.     public JsonClass(string jsonString) {
    7.         name = jsonString;
    8.     }
    9.     public JsonClass() { }
    10. }
    11. [System.Serializable]
    12. public class views
    13. {
    14.     public string href;
    15. }
    I load the json file with following which i found in the forum aswell:
    Code (CSharp):
    1. public class JsonHandler : MonoBehaviour {
    2.  
    3.     [RuntimeInitializeOnLoadMethod]
    4.     IEnumerator Start()
    5.     {
    6.         string url = "anotherURL";
    7.         WWW www = new WWW(url);
    8.         yield return www;
    9.         if (www.error != null)
    10.         {
    11.             Debug.Log("ERROR: " + www.error);
    12.         }
    13.         else
    14.         {
    15.             Debug.Log("No Error");
    16.             Debug.Log(www.text);
    17.             Debug.Log(ProcessJson(www.text));
    18.                    }
    19.  
    20.     }
    21.  
    22.     private string ProcessJson(string jsonString) {
    23.         JsonClass parsejson = new JsonClass();
    24.         parsejson = JsonUtility.FromJson<JsonClass>(jsonString);
    25.         return parsejson.name;
    26.     }
    The name is overwritten right but the views are empty. How can i handle this? I already searched in the Forum but didn´t found a working solution.
     
    Last edited: Jan 23, 2018
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Okay, your code looks okay, though I have a few questions:

    1) In ProcessJson(string jsonString) it's specifically only returning the name, and I don't see parseJson variable being saved anywhere. So how are you checking the views don't exist. Is it definitely being saved?

    2) What is the output of "www.text". Does it contain correctly formatted views as a json string?
     
  3. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    Try making the 'views' class into a struct or using List<views> instead of views[]

    I don't think the Unity Json parser is that robust compared to others, I usually use other 3rd party solutions.
     
  4. Chris-HG

    Chris-HG

    Joined:
    Aug 10, 2012
    Posts:
    63
    Are you doing this at runtime or in the editor?