json字符串转json格式 不改变顺序的方法:

var json_text = '{"125":{"title":"香蕉","amount":"122"},"122":{"title":"香蕉2","amount":"12"},"total":{"title":"所有香蕉","amount":"123"}}'
//1.先把字符串分割成数组
var arr = json_text.split("},");
var segment = [];
//2.遍历每个数组元素并转成单个json,最后组合到segment里
arr.forEach(function(item,index){
    if(index==0){
        segment.push(JSON.parse(item+"}}"))
    }else if(index==(arr.length-1)){
        segment.push(JSON.parse("{"+item))
    }else{
        segment.push(JSON.parse("{"+item+"}}"))
    }
    
})
console.log(segment)

你可能感兴趣的:(JavaScript,json,vue.js,前端)