Search Unity

Getting a JSON Array's Name

Discussion in 'Scripting' started by AV_Corey, May 23, 2018.

  1. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    I need to return the name of my array from my JSON database but I can't figure out how.



    From the image above I just need to return the "Headgear" part as a string but I can't find a way to 'access' it from another script.
    Any help would be appreciated.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    With json, you get a json string that you deserialize into a variable that is a type of the class, so then you can access it by the variable. In your case, you would have a list in a class that would be "Headgear".

    You don't get the Headgear as a string, you just cast it and access Headgear variable.
     
  3. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    So I currently have a script on every item that grabs all the details/variables of that item from the JSON database. One of these variables (on every item) is itemType which can be Headgear, Chestpiece, Gloves etc.

    When the player selects the item in their inventory I want to use the itemType variable on the item to know which equipment slot to place the item into. For example, if it has the itemType of Headgear it will place the item into the Headgear equipment slot.

    I can achieve this easily by adding: ' "Type": Headgear, ' to every Headgear item in the JSON database but wanted to grab the itemType from the name of the array for readability. (saves adding countless lines of code that are the exact same)

    The itemType variable is a string, so is it possible for me to access/convert the "Headgear" variable from the JSON database to a string for this purpose?

    Sorry if I'm not understanding you fully, I'm new to using JSON :/
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    http://json2csharp.com/

    So if you paste your json string into this it will show you what your classes are. In your case. Headgear would be a variable, so it should show you a List with Headgear as the name of that list. So, you could easily create several list if you wanted and check what list the item is in or you add a variable for slot.

    Normally speaking you don't really keep the json string around once you deserialize it, and if you do, you'd have to parse it yourself to get values.

    What is best for you, I'm not sure, but you aren't going to be able to access the word "Headgear" very easily if you are just saying, I have this item and want to know what type it is under by having to parse the json string yourself.
     
  5. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    Thankyou for the explanation :D

    So is there a way to simply check which list an item is in? For example finding out what list ‘S***ty Helmet’ is in.

    If not then I will add a Slot: variable to each item in my JSON database and just deal with the extra lines of code.

    Again, thankyou for the help :)
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    There are ways to search list, but it might be better to just track what category the item belongs to.