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
Add a ment  | 

2 Answers 2

Reset to default 5
return 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