如何将json对象转成数组???

如何将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","年龄"]

阅读 32.8k
7 个回答

这个是你要的

[key,value,name,value, ...]

const list = []
Object.keys(json).forEach(function(item, index) { 
    this.push( item, json[item])
}, list)

但我想你要是循环渲染应该要这样好用些(需要就直接循环渲染就可以,对应的值就是key 和 value)

[{},{},{}]

const list = Object.keys(json).map((item, index) => ({key: item, value:json[item]}))
var result = [];
for (var i in json) {
    result.push(i);
    result.push(json[i])
}
function jsonToArray (str) {
    const obj = JSON.parse(str);
    const result = [];
    for (let key in obj) {
        result.push(key);
        result.push(obj[key]);
    }
    return result;
}
let arr = [];
for(let i in a){
        arr.push(i,a[i]);
    }
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(/^"|"$/,''))

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏