Parse JSON Question
-
Howdy folks, I have a JSON object named after each Expansion and need to access it directly:
{ "Expansion" : 1.1, }
Since it's a string, I can't do something like this:
const myExpansion = expHandler.getCurrentExpansion(); reg myExpansionName = myExpansion.getProperties().Name; // "Expansion" Console.print("Latest Version: " + myJSONObject.myExpansionName); //doesn't work because "Expansion" is a string
Is there a variant of
eval
/parseInt
/parseAsJSON
for this? None of them seem to work for this particular case. -
Type parseJSON into the forum search ;)
-
@d-healey I obviously need a t-shirt cause I still can't get it working.
Is there any other way to just index JSON? I see examples of that on stackoverflow and since I already have all of the relevant indexes in a loop it would fix everything
-
@iamlamprey Can you give an example that isn't working?
-
@d-healey I think I might have it backwards with the whole Parse thing.
I just need to take this:
reg myString = "ExpansionName"; // i would get this from getProperties() const myObj = { "ExpansionName" : 1.1, }
and do this:
Console.print(myObj.myString); Console.print(myObj.ExpansionName); // Works Console.print(myObj."ExpansionName"); // Doesn't work (obviously)
So I need to remove the " " from the string somehow, the following all just return
undefined
so I think I'm looking in the wrong places:parseInt(myString ) // duh eval(myString) myString.ParseAsJSON();
-
@iamlamprey It's an object. You can access properties either using
.
or using[]
-
@d-healey Ahh there we go, I didn't try passing the string as an Index.
const myObj = { "ExpansionName" : 1.0, } const myString = "ExpansionName" Console.print(myObj[ExpansionName]);
Cheers
-
btw how does
const
vsconst var
work? Is it just saving 3 keystrokes? Or is there something going on in the background that is more/less efficient? -
@iamlamprey just three keystrokes.