关于jquery遍历json数据的问题 10C

[{"1":["成都","绵阳","德阳","内江"]},{"2":["南京","无锡","徐州"]},{"3":[.........]}]
请问下,这样的一组json数据我要怎么去解析遍历他?得到单个的市。

10个回答

通过pyth

$.each(Data, function(idx, obj) {
                alert(idx+ "   "+ obj );
            });......
答案就在这里:jquery遍历json数据
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?

Sky_11RJ
BlueSky杰 这种方式不行的,都试过了
4 年多之前 回复

$.each(Data, function(idx, obj) {
alert(idx+ " "+ obj );
});......
是可以,不这,你取出来后还是一个json.还得这么取。obj.1/obj.2

    var  citys =[{"1":["成都","绵阳","德阳","内江"]},{"2":["南京","无锡","徐州"]},{"3":[.........]}]   ;
             var name = $("#city") 
            $.each(Data, function(idx, obj) {
           if( idx.2[obj]  = name){
                           alert("obj");
                            alert(idx.2[obj])
                        }
        });......

用even("")试试

var arr=new Arry();
arr = event([{"1":["成都","绵阳","德阳","内江"]},{"2":["南京","无锡","徐州"]},{"3":[.........]}]) ;

arr[0].1 ="成都","绵阳","德阳","内江" ;

说错了,不是event 是eval 哈哈
var arr=new Array();
arr = event([{"1":["成都","绵阳","德阳","内江"]},{"2":["南京","无锡","徐州"]},{"3":[.........]}]) ;

arr[0].1 ="成都","绵阳","德阳","内江" ;

接着
var arrCity =new Array() ;
arrCity = arr[0].1.split(',');//根据逗号分割;
arrCity[0] =成都;
arrCity[1] =绵阳;
..........

说实话 你这个格式有点诡异 但是能解析 ;下面解析后城市都在cityArr数组里, 已测试无报错;

var cityArr=[];//存储城市
var a = '[{"1":["成都","绵阳","德阳","内江"]},{"2":["南京","无锡","徐州"]}]';
var o = JSON.parse(a);//Object
for(var i in o){
var arr=[];
for(var j in o[i]){
if(o[i][j]) arr = o[i][j]
}
if(arr){
for(var k=0;k<arr.length;k++){
cityArr.push(arr[k])
}
}

    }

    var data = [{ "1": ["成都", "绵阳", "德阳", "内江"] }, { "2": ["南京", "无锡", "徐州"] }]
    for (var i = 0; i < data.length; i++) {
        for (var attr in data[i]) {
            var arr = data[i][attr];
            for (var j = 0; j < arr.length; j++)
                alert(arr[j]);
        }
    }

var ls = [{ "1": ["成都", "绵阳", "德阳", "内江"] }, { "2": ["南京", "无锡", "徐州"] }]
var citys = []
ls.map((value, i) => {
value[i + 1].map((a) => {
citys.push(a)
})
})
console.log(citys)

    亲测有效!
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问