admin管理员组文章数量:1287942
I have a function which returns an object of form:
[{"key":"name","value":"ali","key":"age","value":"56"}]
when called as given below. How can I have it return same kind of object but without the square brackets?
setProperties('{"name":"ali","age":"56"}');
function setProperties(str) {
var properties = [];
var json = jQuery.parseJSON(str);
for (property in json) {
properties.push({
key: property,
value: json[property]});
}
return properties;
}
I have a function which returns an object of form:
[{"key":"name","value":"ali","key":"age","value":"56"}]
when called as given below. How can I have it return same kind of object but without the square brackets?
setProperties('{"name":"ali","age":"56"}');
function setProperties(str) {
var properties = [];
var json = jQuery.parseJSON(str);
for (property in json) {
properties.push({
key: property,
value: json[property]});
}
return properties;
}
Share
Improve this question
edited Apr 20, 2012 at 5:35
Ryan
14.7k8 gold badges67 silver badges104 bronze badges
asked Apr 20, 2012 at 5:08
UthmanUthman
9,83719 gold badges79 silver badges109 bronze badges
2 Answers
Reset to default 5return properties[0]; // returns the first element of the list instead of the whole list
The square brackets indicate an Array literal, so if you just select the first element of the Array: [{"name":"ali","age":"56","height":"xyz"}][0]
it returns the Object you want.
本文标签: javascriptReturning a json object without square bracketsStack Overflow
版权声明:本文标题:javascript - Returning a json object without square brackets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741250977a2365781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论