admin管理员组

文章数量:1287134

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?

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?

Share Improve this question edited Apr 21, 2016 at 4:42 T J 43.2k13 gold badges86 silver badges142 bronze badges asked Apr 21, 2016 at 4:33 RajeshwarRajeshwar 3812 gold badges7 silver badges20 bronze badges 6
  • Show us the expected output! – Rayon Commented Apr 21, 2016 at 4:37
  • show the expected way and output that you want ? if you want to parse just like search by the keys ,you can use loadash before search the object .. – masadi zainul Commented Apr 21, 2016 at 4:40
  • try to use underscore functions, underscorejs/#pluck – Ayubxon Ubaydullayev Commented Apr 21, 2016 at 4:42
  • The post claims to have a [JavaScript] object. JSON.parse takes JSON text. A useful start would be to omit the incorrect JSON.parse usage - edit the post to remove the incorrect information. – user2864740 Commented Apr 21, 2016 at 4:42
  • Are you sure the objects in array is supposed to have different keys? If those values are of same type, you should use same key name and it'll be much simpler. – T J Commented Apr 21, 2016 at 4:47
 |  Show 1 more ment

6 Answers 6

Reset to default 5

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./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);
}

本文标签: parse json object in javascript to get key and valuesStack Overflow