Search Unity

How to create a empty JSONNode?

Discussion in 'Scripting' started by Shadowing, Aug 18, 2019.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I'm having problems figuring out how to create a empty JSON Node

    If i give it a parse empty bracket string like this it works. Gotta be another way?
    Code (csharp):
    1. JSONNode m_JSONNode = JSON.Parse("{}");
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Just use JsonUtility. See Unity documentation for details.
     
  3. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Thanks for the reply Antypodish

    Use JsonUtility to create a empty Json?

    I'm really looking for a way to do it with in the JSONNode class.
    I just checked JSONUtility documentation I don't see anything that covers this.
    Seems id have to do it the same way as how im doing now parsing a two empty brackets as a string.
     
  4. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Here is what im trying to do

    This below gives me a error saying m_JsonNode is null

    Code (csharp):
    1.  
    2. JSONNode m_JsonNode;
    3.  
    4. m_JsonNode['test'] = 1234;
    5.  
    6.  
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Where you got JsonNode from?
    If you downloaded it from somewhere, read its documentation.

    JsonUtility is dead simple to use. It has tons of examples. Just search web, or forum.

    Btw. In your last example, you are acting upon null class of JsonNode. You need create it first. Or use parse as in first example.

    In JsonUtility you create your desired class tree, to be parsed to Json or from Json.
     
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Ya always check documentation before posting. It's not in the documentation / examples.
    Ya I haven't used JasonUtility before.

    I really like simple json.
    Idk what data types it let's you store but simple json even let's you store vector3 and other stuff.

    https://wiki.unity3d.com/index.php/SimpleJSON
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    I am sure internet is also full of examples for SimpleJSON. Even it has some example on the page. Just copy it directly.
    Yet, discussed tool was last time updated 29 November 2017. So not sure, if there is any intended further maintenance, in case it stop working for some reason one day, with latest Unity (2020s maybe?:) ).
    However, I didn't bother to look for anything else, since Unity has built in JSON tools.

    JSONUtility allows you to parse whole class to/form. Doesn't matter what is inside class. Main restriction are dictionaries.
    Other than that, most data types are serialized.
    Major benefit is, it is directly supported by Unity out of box.
    I strongly suggest give it go.
     
  8. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Idk i just read about JSONUtility
    JSONNode seems way better. Looks like you have to create a class for everything.

    I like SimpleJSON due to how loose it is.
    It doesn't care about anything really.

    Code (csharp):
    1.  
    2. JSONNode m_JsonNode;
    3.  
    4. m_JsonNode["Type of trucks"] [0] = "Red";
    5. m_JsonNode["Type of trucks"] [1] = "Blue";
    6. m_JsonNode["Type of trucks"] [2] = "Yellow";
    7. m_JsonNode["Type of trucks"] [3] = 200; // yup you can just throw in a int if you want. idk why anyone would do that though ha. Either way you type cast it when you read the data.
    8.  
    9. string myTruck = m_JsonNode["Type of trucks"] [1].Value;
    10. //Value returns a string. other wise its .int or .float or .Vector3 if you working with vectors
    11. string myTruck = m_JsonNode["Type of trucks"] [3].Value; // This would return (string)"200"
    12. string myTruck = m_JsonNode["Type of trucks"] [1].int; // This would return (int)0
    13.  
    14. Or you can go super deep
    15. m_JsonNode["Type of trucks"]["Ford"]["color"]["year"][2000][0] = "F1100";
    16.  
    17.  

    JSONUtlity looks like it lets you store any Class Type though so thats a huge advantage with that.
     
    Last edited: Aug 18, 2019
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    How you pass data to class then?

    You need pass data to/from class at some point. With JsonUtility you do that with single line of code, while in either approach you will take, you still need to create a class (es), to store data anyway.
     
  10. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Code (csharp):
    1.  MyClass.m_truck = m_JsonNode["Type of trucks"] [1].Value;
    assign to anywhere you want.

    You can use it with out assigning it to anything

    Code (csharp):
    1.  
    2. if(m_JsonNode["Type of trucks"] [1].Value == "Red"){
    3.  
    4. }
    5.  
    6.  
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Well, is your choice. If it works for you it works. That is fine.
    I think you got things figured out by now anyway :)

    Edit:
    Just a thought, besides JSON.
    Try convert values to enumerator rather a string. While at initialization is fine, you don't want IF'ing strings in Update.
     
  12. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    SimpleJSON is well used but that may be due to also JSONUtility only being a few years old.
    It didn't exist when I started using Unity back in version Unity 4.3.

    Really good for Unknown data too. I mostly use it for data coming from my web server.

    I don't understand from reading the documentation on JSONUtility how you cycle through the JSON data?

    Looks like its done like this
    Code (csharp):
    1.  PlayerInfo P = JsonUtility.FromJson<PlayerInfo>(json);
    Looks like you have to save the object into a variable before you can even do anything with it.
    See you can loop through data inside JSONNode like this below.

    The Syntax is much cleaner too

    Code (csharp):
    1.  
    2. foreach(JSONNode Truck in m_JsonNode["Type of trucks"]){
    3.  
    4.        if(Truck.Value == "Red"){
    5.  
    6.                    // ahh my truck
    7.        }
    8. }
    9.  
    Lets just grab a deep array and loop through it. Ofcourse you do assign it to a variable for performance reasons.
    Code (csharp):
    1.  
    2.  
    3. JSONNode Years = m_JsonNode["Type of trucks"]["Ford"]["color"]["year"][2000];
    4. // not that im saying it would be slow just be silly not to assign it first.
    5.  
    6. foreach(JSONNode Truck in m_Years ){
    7.  
    8.        if(Truck.Value == "F1100"){
    9.  
    10.                    // ahh my truck
    11.        }
    12. }
    13.  
     
  13. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Yes, JsonUtility is rather young. I think first time came in 2017? That why you got so many other JSON libraries available form before that time.

    You don't need to loop, as all json is parsed into your class. Class can hold other classes, arrays, lists, and other common data types. It does not parse dictionary. But you can convert to dictionary afterwards, if needed.

    Then you access data as from any other class object. for example:
    Code (CSharp):
    1.  
    2. MyClassFromJson myClassFromJson = JsonUtility.FromJson<MyClassFromJson>(json);
    3.  
    4. var color = myClassFromJson.myTruckColor ;
    5. List <Truck> trucks = myClassFromJson.availableTrucks ;
    etc.
    Code (CSharp):
    1. foreach ( var truck in trucks )
    2. {
    3. ... do something
    4. }