如何将json对象转成数组???
json对象:
{"phone_num_wxx":"用户手机号","phone_city_wxx":"手机号归属地","sex_wxx":"性别","income_wxx":"收入","age_wxx":"年龄"}
希望转换成这样的数组形式:
["phone_num_wxx","用户手机号","phone_city_wxx","手机号归属地","sex_wxx","性别","income_wxx","收入","age_wxx","年龄"]
如何将json对象转成数组???
json对象:
{"phone_num_wxx":"用户手机号","phone_city_wxx":"手机号归属地","sex_wxx":"性别","income_wxx":"收入","age_wxx":"年龄"}
希望转换成这样的数组形式:
["phone_num_wxx","用户手机号","phone_city_wxx","手机号归属地","sex_wxx","性别","income_wxx","收入","age_wxx","年龄"]
function jsonToArray (str) {
const obj = JSON.parse(str);
const result = [];
for (let key in obj) {
result.push(key);
result.push(obj[key]);
}
return result;
}
const obj = {
"phone_num_wxx": "用户手机号",
"phone_city_wxx": "手机号归属地",
"sex_wxx": "性别",
"income_wxx": "收入",
"age_wxx": "年龄"
};
let arr = [];
Object.keys(obj).forEach(v => {
let o = {};
o[v] = obj[v];
arr.push(o)
})
let obj = {"phone_num_wxx":"用户手机号","phone_city_wxx":"手机号归属地","sex_wxx":"性别","income_wxx":"收入","age_wxx":"年龄"};
let list = [];
list.push(obj);
得到的是对象数组,猜测你需要的是这种
[{},{},{}]
如果是楼主描述的简单的json对象 可以直接用字符串替换
JSON.stringify(jsonObj).replace(/\{|}/g, '').replace(/:/g, ',').split(',').map(e => e.replace(/^"|"$/,''))
8 回答5.8k 阅读✓ 已解决
9 回答9.1k 阅读
6 回答4.7k 阅读✓ 已解决
5 回答3.5k 阅读✓ 已解决
12 回答5.7k 阅读
4 回答7.9k 阅读✓ 已解决
7 回答9.7k 阅读
这个是你要的
但我想你要是循环渲染应该要这样好用些(需要就直接循环渲染就可以,对应的值就是key 和 value)