简体   繁体   中英

convert json object to array

I have a JSON object like this coming back as a server response.

{"names":["Kreisler","Kreisler","Kreisler"]}

I need this object as an array.

Any help?

Use JSON.parse() :

var json = '{"names":["Kreisler","Kreisler","Kreisler"]}';
data = JSON.parse(json);
console.log(data['names']); // ["Kreisler", "Kreisler", "Kreisler"]

考虑到响应数据属于response变量,请使用以下命令:

var names = response["names"];
var data = JSON.parse('{"names":["Kreisler","Kreisler","Kreisler"]}', function(k, v) {
  console.log(k); // log the current property name, the last is "".
  return v;       // return the unchanged property value.
});
console.log(data.names[0]);

如果您在数据类型中使用json参数,并且您的回调参数称为response,那么:

arrayThatINeed = response.names;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM