38

I need to store many values in single key of json. e.g.

{
  "number" : "1","2","3",
  "alphabet" : "a", "b", "c"
}

Something like this. Any pointers?

Gaurav
  • 758
  • 2
  • 13
  • 26

3 Answers3

88

Use arrays:

{
    "number": ["1", "2", "3"],
    "alphabet": ["a", "b", "c"]
}

You can the access the different values from their position in the array. Counting starts at left of array at 0. myJsonObject["number"][0] == 1 or myJsonObject["alphabet"][2] == 'c'

Elliot Bonneville
  • 49,322
  • 23
  • 92
  • 122
  • how to access in json parsing can you describe? –  Dec 11 '14 at 13:48
  • @Johnson `var jsObject = JSON.parse(jsonData);` At this point, the variable `jsObject` can be accessed normally. – Elliot Bonneville Dec 11 '14 at 14:20
  • here is my question can you check it http://stackoverflow.com/questions/27424584/how-to-use-multiple-values-in-single-key-in-json-parsing?noredirect=1#comment43299243_27424584 –  Dec 12 '14 at 03:57
  • @Johnson Sorry mate, but it doesn't look like your problem is JavaScript specific, so I can't help you. – Elliot Bonneville Dec 12 '14 at 04:19
9
{
  "number" : ["1","2","3"],
  "alphabet" : ["a", "b", "c"]
}
marekful
  • 14,178
  • 5
  • 35
  • 56
-6
{
    "success": true,
    "data": {
        "BLR": {
            "origin": "JAI",
            "destination": "BLR",
            "price": 127,
            "transfers": 0,
            "airline": "LB",
            "flight_number": 655,
            "departure_at": "2017-06-03T18:20:00Z",
            "return_at": "2017-06-07T08:30:00Z",
            "expires_at": "2017-03-05T08:40:31Z"
        }
    }
};
Tom-db
  • 6,017
  • 2
  • 25
  • 40
vicky
  • 17