js操作json:拼接json和转换

    var data11 = '{"name":"yyy","alexa":100,"site":"123"}';
    var data12 = '{"age":11,"grade":10}';

    var data21 = '{"name":"zzz","alexa":200,"site":"456"}';
    var data22 = '{"age":21,"grade":22}';

    var obj1 = JSON.parse(data11);//json字符串转js对象
    var obj2 = JSON.parse(data12);

    var obj3 = JSON.parse(data21);
    var obj4 = JSON.parse(data22);

    var result1 = Object.assign({},obj1,obj2) //拼接json对象
    var result2 = Object.assign({},obj3,obj4)
    console.log(result1)
    console.log(result2)
    var last=JSON.stringify(result1)//js对象转js字符串
    console.log(last)

    var sum = []; //数组接收
    sum.push(result1)
    sum.push(result2)
    console.log(JSON.stringify(sum)) 
    console.log(JSON.parse(JSON.stringify(sum)))

你可能感兴趣的:(js操作json:拼接json和转换)