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
6 Answers
Reset to default 5JSON.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 extractkeys
of the object in thecallback
. Iterate them to gain thevalue
of each key usingArray#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
版权声明:本文标题:parse json object in javascript to get key and values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741220808a2360934.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论