• Print

Author Topic: Assigning numeric value to non-sequential table  (Read 4714 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Assigning numeric value to non-sequential table
« on: May 23, 2017, 08:14:57 am »
So I have this strong of JSON:
Code: Text
  1.  
  2. {
  3.       "default": [
  4.          {
  5.             "name": "Default",
  6.             "access": "user",
  7.             "temp": false
  8.          }
  9.       ],
  10.       "en": [
  11.          {
  12.             "name": "English",
  13.             "access": "user",  
  14.             "temp": false
  15.          }
  16.       ],
  17.       "dead": [
  18.          {
  19.             "name": "Dead",
  20.             "access": "user",
  21.             "temp": false
  22.          }
  23.       ],
  24.       "es": [
  25.          {
  26.             "name": "EspaƱol",
  27.             "access": "user",
  28.             "temp": false
  29.          }
  30.       ],
  31.       "admin": [
  32.          {
  33.             "name": "Admins",
  34.             "access": "operator",
  35.             "temp": false
  36.          }
  37.       ]
  38.    }
  39.  
Which I then have converted into a table using util.JSONToTable. I don't have the ability to test it right now (I can in a few hours but not at the moment -- I'll edit this if I can test before someone responds), but how does this _exactly_ work in this case. On the wiki page, it says
Quote
WARNING   
This function converts keys to numbers whenever possible
How would this affect my table? I need to access these tables like "SCC.Channels[ "default" ]" or something like that, but if the command assigns numeric keys, I could access it like "SCC.Channels[ 1 ].name" or something.


Like I said I can test this myself in a couple hours but I can't right now (at school). Is there anyone who knows this for sure?




EDIT: Ran it myself:
Code: [Select]
lua_run tab = util.JSONToTable( '{ "default": [ { "name": "Default", "access": "user", "temp": false } ], "en": [ { "name": "English", "access": "user",   "temp": false } ] }') PrintTable( tab )and this is the output:
Code: [Select]
default:
1:
access   =   user
name   =   Default
temp   =   false
en:
1:
access   =   user
name   =   English
temp   =   false
But I'm having a bit of trouble understanding it, what is the "1:" doing under the "en:" and "default:"? Does that mean it would need to be SCC.Channels[ "default" ][ 1 ].name?
From what I understand, table structure would look like this:
Code: Lua
  1. SCC.Channels = {
  2.     "default" = {
  3.         [1] = {
  4.             "access" = "user",
  5.             "name" = "Default",
  6.             "temp" = false
  7.         }
  8.     }
  9. }
  10.  
Am I correct?


I've never used this tool before so if someone could help me understand, that'd be appreciated.


EDIT2: Now that I think about it, I could use "ULib.parseKeyValues" and "ULib.makeKeyValues" as my addon does (for now, at least) rely on ULib. But I'd still like to understand this as I do want it to be standalone.
« Last Edit: May 23, 2017, 10:20:11 am by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Assigning numeric value to non-sequential table
« Reply #1 on: May 23, 2017, 03:45:04 pm »
The following snippet of JSON:

Code: Text
  1. "default": [
  2.          {
  3.             "name": "Default",
  4.             "access": "user",
  5.             "temp": false
  6.          }
  7.       ],

This converts to an array of objects, and you only have one item in the array. This is why it looks a little strange on the other side. [...] --> array. {...} --> dictionary.
Experiencing God's grace one day at a time.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Assigning numeric value to non-sequential table
« Reply #2 on: May 24, 2017, 08:07:36 am »
Ok... so I tried doing this:
Code: [Select]
{
    "default": {
        "name": "Default",
        "access": "user",
        "temp": false
    }
}
and that seems to work out better. I've never had to use JSON directly before so this is new for me :P
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print