admin管理员组文章数量:1410689
I am having a JSON response as
{ "-0.15323": "" }
How to Parse the -0.15323
part only? I mean say
var json = '{ "-0.15323": "" }'
var obj = JSON.parse(json);
Now
return obj;
should return me -0.15323
only. Slice is not a good option. Because the data may e in variable size.
I am having a JSON response as
{ "-0.15323": "" }
How to Parse the -0.15323
part only? I mean say
var json = '{ "-0.15323": "" }'
var obj = JSON.parse(json);
Now
return obj;
should return me -0.15323
only. Slice is not a good option. Because the data may e in variable size.
3 Answers
Reset to default 5That is a Javascript Object literal.
So you can use the Object.keys function, which is the simpler equivalent of doing a loop through all the enumerable properties with the for-in loop (like in Donal's example):
var ob = {
"-0.15323": ""
};
alert(Object.keys(ob)[0])
or even the Object.getOwnPropertyNames function, which FYI gives access to both enumerable and non-enumerable properties. You can access your property with:
var ob = {
"-0.15323": ""
};
alert(Object.getOwnPropertyNames(ob)[0])
Both are Ecmascript 5, and should be supported in all major browsers.
That json is an object, so you can do something like this:
var obj = { "-0.15323": "" };
var key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key);
}
}
Here is a working example: http://jsfiddle/dndp2wwa/1/
parseFloat(Object.keys({"-1.2345":""})[0])
本文标签: javascriptJSON Parse only objectStack Overflow
版权声明:本文标题:javascript - JSON Parse only object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745009921a2637485.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论