Trouble parsing multi-level JSON
-
03-07-2019 - |
Question
I have a chunk of JSON which looks something like:
{
"map": [
[
"zimbraFeatureCalendarUpsellEnabled",
"FALSE"
],
[
"zimbraPrefCalendarDayHourStart",
"8"
],
[
"zimbraFeatureOptionsEnabled",
"TRUE"
],
[
"zimbraAttachmentsViewInHtmlOnly",
"FALSE"
]
]
}
(and so on; there's 200+ entries)
I need to be able to pick out individual key/value pairs from the JSON response, either with jQuery or plain old Javascript. I haven't been able to figure out how to address a specific key, though. Any ideas?
Solution
What you've described is a single level object, with a whole bunch of nested arrays so accessing will be
myObject.map[entryNumber][0 or 1] // 0 == key, 1 == value
You probably want something akin to this (unless you're working with existing API or some such):
{
"map": {
"zimbraFeatureCalendarUpsellEnabled": "FALSE",
"zimbraPrefCalendarDayHourStart": "8",
...
}
}
OTHER TIPS
Instead of using arrays, you could use an object:
{
map : {
"zimbraFeatureCalendarUpsellEnabled" : "FALSE",
"zimbraPrefCalendarDayHourStart" : "8",
"zimbraFeatureOptionsEnabled" : "TRUE",
"zimbraAttachmentsViewInHtmlOnly" : "FALSE"
}
}
and then to access it:
myJSONObject.map.zimbraFeatureCalendarUpsellEnabled;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow