json转换格式

发布网友 发布时间:2022-04-23 05:19

我来回答

3个回答

热心网友 时间:2022-04-18 09:00

var results = [], arr = {} //arr就是你的json数组,results是转后的结果;
for (var i in arr.success) {
var objs = arr.success[i];
for(var j in objs){
if(results[j]==undefined){
results[j]=[];
}
var obj = objs[j];
results[j].push(obj.t)
}
}

以下是运行结果,你的json代码有重复的C1

热心网友 时间:2022-04-18 10:18

不多说了,直接上代码!

var json={
                "success": {
                    "1": [
                        {
                        "t": "A1",
                        "c": "2017-06-28 19:02:46"
                        },
                        {
                        "t": "A2",
                        "c": "2017-06-28 19:02:46"
                        }
                    ],
                    "2": [
                        {
                        "t": "B1",
                        "c": "2017-06-28 18:59:21"
                        },
                        {
                        "t": "B2",
                        "c": "2017-06-28 18:59:21"
                        }
                        ],
                    "3": [
                        {
                        "t": "C1",
                        "c": "2017-06-28 19:00:27"
                        },
                        {
                        "t": "C2",
                        "c": "2017-06-28 19:00:27"
                        }
                    ]
                }
            };
            var arr=json.success["1"],re_arr=[],narr=[];
            for(var key in json.success){
                narr=json.success[key] || [];
                for(var i=0,len=narr.length;i<len;i++){
                    if(!re_arr[i] || !re_arr[i] instanceof Array){      //判断当前re_arr[i]是否为数组
                        re_arr[i]=[narr[i]["t"]];       //将re_arr[i]设置为数组
                    }else{
                        re_arr[i].push(narr[i]["t"]);       //push数组
                    }
                }
            }
            console.log(re_arr);

追问大兄弟,你的arr没用到,不过效果好像就是我要的,谢谢了

热心网友 时间:2022-04-18 12:10

没太懂你意思,【A1,B1,C1】对应什么

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com