返回没有方括号的json对象

cho*_*hoc 1 javascript jquery

我有一个返回形式对象的函数: [{"key":"name","value":"ali","key":"age","value":"56"}]当调用如下给出时.我怎么能让它返回相同类型的对象但没有方括号?

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;
}
Run Code Online (Sandbox Code Playgroud)

tob*_*ies 5

return properties[0]; // returns the first element of the list instead of the whole list
Run Code Online (Sandbox Code Playgroud)


jla*_*eda 5

方括号表示数组文字,因此如果您只选择数组的第一个元素:[{"name":"ali","age":"56","height":"xyz"}][0]它将返回您想要的对象.