-3

My json data format is like below:

"INFO" : {
    "DETAILS" : {
        "EMP" : {
             "amount": " 12185",
             "job": "GAPA",
             "month": "JANUARY",
             "year": "2010"
         }
     }
},

I am getting the values of keys in the above json. After getting the key values I want to get the map (i.e) INFO.

Now, I want to set the id or a attribute value as "INFO" for a button like submit.

How can I do that??

BeNdErR
  • 17,471
  • 21
  • 72
  • 103
user3271762
  • 23
  • 1
  • 9

2 Answers2

3

It's not entirely clear what you're asking, but if you want to loop through the items, you can do this.

var json = '{"INFO" : {"DETAILS" : {"EMP" : {"amount": " 12185","job": "GAPA","month": "JANUARY","year": "2010"}}}}',
  i;

json = JSON.parse(json);

for (i in json.INFO.DETAILS.EMP) {

  // always check for own properties when looping through an object
  if (json.INFO.DETAILS.EMP.hasOwnProperty(i)) {
    console.log('key: %s, value: %s', i, json.INFO.DETAILS.EMP[i]);
  }
}
reergymerej
  • 2,371
  • 2
  • 26
  • 32
1
for(var key in data){
    // key is "INFO" with your example
}

the var key assumes the values of all the keys of your object (only the first level, with your example the for ends after one cycle)

Example: http://jsfiddle.net/Sam88/tk8XP/

Sam
  • 166
  • 5
  • can i have a example related to it for better understanding.. – user3271762 Feb 14 '14 at 17:08
  • first for logs "INFO", second for logs all the keys in EMP http://jsfiddle.net/Sam88/tk8XP/ – Sam Feb 14 '14 at 17:12
  • I used your solution. However, am getting the first key only INFO all the time when am implementing. Its not moving to other key in the object(i.e) INFO2,..Check the fiddle here http://jsfiddle.net/tk8XP/3/ – user3271762 Feb 15 '14 at 17:34
  • sorry but I don't understand what you are trying to do. If you use data.INFO you are going to get only DETAILS, if you use data instead you are going to get INFO, INFO2, etc. – Sam Feb 17 '14 at 08:46