search

Home  >  Q&A  >  body text

Data structure - How to combine json data into an object array in javascript?

like

{"name":"`111","password":"111","title":"111","tag":"111","contents":"1111"},{ "name":"222","password":"22","title":"22","tag":"22","contents":"222"},{"name":"11", "password":"11","title":"11","tag":"11","contents":"11111"}

Converted to

[{"name":"`111","password":"111","title":"111","tag":"111","contents":"1111"}, {"name":"222","password":"22","title":"22","tag":"22","contents":"222"},{"name":"11" ,"password":"11","title":"11","tag":"11","contents":"11111"}]

To add, this is the data transmitted from the backend to the frontend

迷茫迷茫2790 days ago815

reply all(4)I'll reply

  • 迷茫

    迷茫2017-05-19 10:13:19

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-19 10:13:19

    // 把数据往里面一扔
    var tx = {
        a: {"name": "`111", "password": "111", "title": "111", "tag": "111", "contents": "1111"},
        b: {"name": "222", "password": "22", "title": "22", "tag": "22", "contents": "222"},
        c: {"name": "11", "password": "11", "title": "11", "tag": "11", "contents": "11111"}
    };
    var sb = [];
    // 遍历栈入
    for (var tb in tx) {
        sb.push(tx[tb])
    }
    console.log(sb[]);

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-19 10:13:19

    I improved the code upstairs and got the effect the poster wanted

    var obj = {
        a: {"name": "111", "password": "111", "title": "111", "tag": "111", "contents": "1111"},
        b: {"name": "222", "password": "22", "title": "22", "tag": "22", "contents": "222"},
        c: {"name": "11", "password": "11", "title": "11", "tag": "11", "contents": "11111"}
    };
    var result = [];
    for (var key in obj) {
        result.push(obj[key]);
    }
    console.log(JSON.stringify(result));

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-19 10:13:19

    What you mean is:
    Put the data in the .json file sent from the backend into the new object array on the frontend.
    Ajax is required for transmission (an example is the ajax method of jquery). You can also try axios, which is more popular now.

    Assumptions:
    1. The file that needs to be passed in is test.json
    2. The data content of test.json is

    {
        "userone":{"name":"111","password":"111","title":"111","tag":"111","contents":"1111"},
        "usertwo":{"name":"222","password":"22","title":"22","tag":"22","contents":"222"},
        "userthree":{"name":"11","password":"11","title":"11","tag":"11","contents":"11111"}
    }

    Conversion:
    1. Obtain data through ajax, and the obtained content is stored in data
    2. Traverse the json data and save it into a new object array, and perform it in the success function

    function getJson(){
            $.ajax({
                type:"GET",
                url:"test.json",
                dataType:"json",
                success:function(data){
                    var jsonData=data;
                    var newObject=[];
                    for (var key in jsonData) {
                        newObject.push(jsonData[key])
                    }
                    JSON.stringify(newObject);
                    console.log(newObject);
                }
            })
    }
    getJson();

    reply
    0
  • Cancelreply