简体   繁体   中英

list only all keys including nested from json object in jquery

I want to list all the keys(including nested) in from json object in jquery... the keys/ json object is dynamic so program never the format but it will be json for sure

I need a jquery that list only keys from JSON

Following is sample data:

{
  "status_code": 200,
  "status_text": "Success",
  "count": 6,
  "data": [{
    "date": "2012-02-27",
    "zone": "NORTH ZONE",
    "centre": "CHANDIGARH",
    "commodity": "Tea Loose",
    "price": "265"
  }, {
    "date": "2012-02-24",
    "zone": "NORTH ZONE",
    "centre": "CHANDIGARH",
    "commodity": "Tea Loose",
    "price": "265"
  }]
}

From above JSON I want to list only keys.

var jsonArray={ "status_code": 200, "status_text": "Success", "count": 6, "data": [ { "date": "2012-02-27", "zone": "NORTH ZONE", "centre": "CHANDIGARH", "commodity": "Tea Loose", "price": "265" }, { "date": "2012-02-24", "zone": "NORTH ZONE", "centre": "CHANDIGARH", "commodity": "Tea Loose", "price": "265" } ] }
 var L = jsonArray.length;
for (var i = 0; i < L; i++) {
var obj = jsonArray[i];
for (var j in obj) {
    alert(j);
}
}

Try this it will works .

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM