简体   繁体   中英

parse json object in javascript to get key and values

I have a object, need to parse the below data

  var data= [{"obj1":"2122"},{"obj2":"123"}]

to get both the keys and values in javascript. I yried to use:

var obj = JSON.parse(data);
for(var prop in data) {
if(data.hasOwnProperty(prop))
  console.log(prop);
}

The values that are obtained in console are

Object {obj1: "2122"}
Object {obj2: "123"}

But I need to access the values seperately and not as object. How to retrieve it from that object?

JSON.parse is use to parse JSONString to Javascript Object.

You can not use it directly on a JavaScript Object ...

Anyway, your object is an array so you may do :

var arr = JSON.parse(data);
arr.forEach(function(elementObject){
    var keys = Object.keys(elementObject);
    keys.forEach(function(key){
      console.log(key + ":"+elementObject[key]);
    })
});

Cheers

Here you will get the values in array "values".

 var data= [{"obj1":"2122"},{"obj2":"123"}] data = JSON.stringify(data); var values = []; JSON.parse(data, function (key, value) { if (typeof(value) != "object") { values.push({[key]:value}); // values.push(value); //if you need a value array } }); 

Use Array#map and extract keys of the object in the callback . Iterate them to gain the value of each key using Array#forEach

 var data = [{ "obj1": "2122" }, { "obj2": "123" }]; var op = data.map(function(item) { var keys = Object.keys(item); var arr = []; keys.forEach(function(key) { arr.push(item[key]); }); return arr; }); console.log(op); 
 <script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script> 

Try this code use $.each function to parse object..

  var data= [{"obj1":"2122"},{"obj2":"123"}] $.each(data, function(key, val) { $.each(val, function(k, v) { console.log('key ='+k); console.log('value ='+v); alert('key = '+k+', value = '+v); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 


Try this one..

var obj = JSON.parse('[{"obj1":2122},{"obj2":123}]');
obj.forEach(function(ElementObject){
var keys=Object.keys(ElementObject);
 console.log(keys[0],ElementObject[Object.keys(ElementObject)]); 
}
);

JsFiddle

First:

var data = [{"obj1":"2122"},{"obj2":"123"}]

This line will create an Array. No need for:

var obj = JSON.parse(data);

If you want to access via key/value you need to restructure your data.

var data = [{key:"obj1",value:"2122"},{key:"obj2", value:"123"}];
for( var index in data)
{ 
  var item = data[index]; 
  console.log(item.key);
  console.log(item.value);

}

Alternately you can map:

 var keys = [{
  "obj1": "2122"
}, {
  "obj2": "123"
}];
var op = data.map(function(item) {
  var keys = Object.keys(item);
  var arr = [];
  keys.forEach(function(key) {
    console.log(key);
    arr.push(key);
  });
  return arr;
});
var i=0;
for(var i=0;i<op.length;i++)
{
  console.log(i);
  var pair=data[i];
  console.log(pair);
  var key=op[i][0];
  console.log(key);
  var value=pair[key];
  console.log(value);
}

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