views:

327

answers:

2

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?

+3  A: 

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;
nickf
One of the delightful idiosyncrasies of JS/JSON mean that JSON properties must always be quoted (JSON is more strict than JS engines as the JSON developers wanted to ensure that there was a single format, but reserved words, symbols, etc aren't valid js property names without quotes)
olliej
thanks - i've edited now to reflect this
nickf
+3  A: 

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",
         ...
    }
}
olliej