{
"Meta Data": {
"1: Symbol": "MSFT",
"2: Indicator": "Triple Exponential Moving Average (TEMA)",
"3: Last Refreshed": "2017-06-26 16:00:00",
"4: Interval": "15min",
"5: Time Period": 10,
"6: Series Type": "close",
"7: Time Zone": "US/Eastern"
}
}
Let me first start off by saying that I just started learning JSON and AJAX, so this may sound like a stupid question. Recentely though, I found this JSON output text. But there a couple of problems I have with it. First of all, some keys have two words in them, without a _ . How can you then use them in javascript? I mean you cannot have a method with a space in it, right? And my second problem is related to the first problem, since there are also some keys that even start out like this 1: or 2: . What's up with that? So if anyone can show me how I can for example reach the "5: time period" key in javascript, that would be a huge help.
The keys in this case are just strings, so you would access them using strings. Assuming you've loaded this JSON into a val
variable, you could use
val["Meta Data"]["1: Symbol"]
The reason you don't need a _
is because anything between quotes in JSON is a string, and all the characters within are part of that same string object.
You can access JSON properties in JavaScript either using bracket notation ([]
) or object notation (object.property
). In this case, since your property is a string with a space, you'd have to use bracket notation.
So, assuming you have this object stored as a variable (say obj
for example purposes), you could get 5: Time Period
by doing obj["5: Time Period"]
.
Hope that helps!