Gaurav
Gaurav

Reputation: 788

Store multiple values in single key in json

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?

Upvotes: 42

Views: 177511

Answers (3)

vicky
vicky

Reputation: 11

{
    "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"
        }
    }
};

Upvotes: -9

Elliot Bonneville
Elliot Bonneville

Reputation: 53331

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'

Upvotes: 97

marekful
marekful

Reputation: 15351

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

Upvotes: 10

Related Questions