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
業(yè)精于勤,荒于嬉;行成于思,毀于隨。
// 把數(shù)據(jù)往里面一扔
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[]);
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));
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();