2 qq 25884459 qq_25884459 于 2017.09.15 15:08 提问

Js循环json数组 求大佬帮助

{
"obj": "{\"cust_info\":[{\"CUST_NAME_FULL\":\"李莉\"}],\"cycle\":[\"0\"],\"error\":\"0\",\"countByHour_mx\":\t[{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"16\",\"YSJE\":\"0.10\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170821102550\",\"JFJE\":\"0.10\",\"BJHM\":\"13009907707\",\"JFCS\":\"1\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"72\",\"YSJE\":\"0.20\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170821150525\",\"JFJE\":\"0.20\",\"BJHM\":\"13009907707\",\"JFCS\":\"2\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"87\",\"YSJE\":\"0.20\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170822104713\",\"JFJE\":\"0.20\",\"BJHM\":\"13009907707\",\"JFCS\":\"2\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"20\",\"YSJE\":\"0.10\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170822105901\",\"JFJE\":\"0.10\",\"BJHM\":\"13009907707\",\"JFCS\":\"1\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"75\",\"YSJE\":\"0.20\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170822133935\",\"JFJE\":\"0.20\",\"BJHM\":\"13009907707\",\"JFCS\":\"2\",\"YHJE\":\"0.00\"},,{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"45\",\"YSJE\":\"0.10\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170823175811\",\"JFJE\":\"0.10\",\"BJHM\":\"13009907707\",\"JFCS\":\"1\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"163\",\"YSJE\":\"0.30\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170820153942\",\"JFJE\":\"0.30\",\"BJHM\":\"13q009907707\",\"JFCS\":\"3\",\"YHJE\":\"0.00\"}],\"error_message\":\"\",\"countByHour_hj\":[{\"SYLX\":\"48\",\"THSC\":\"3719\",\"YSJE\":\"9.10\",\"HDZS\":\"48\",\"JFJE\":\"9.10\",\"JFCS\":\"91\",\"YHJE\":\"0.00\"}]}"
}

这个是Json 我想把这个数组循环出来

5个回答

showbo
showbo   Ds   Rxr 2017.09.15 16:30
已采纳

你的这个字符格式有问题。。

这里有2个逗号,没有办法转成json的,是你打错了还是返回的就是这样,返回就这样需要自己去掉。如果格式错很多只能正则了
 .\":\"0.00\"},,{\"SYLX\":\

    //如果你贴出来的那串是字符串,那么下面这句改为var o=JSON.parse(存储字符串的变量名);
    var o = { "obj": "{\"cust_info\":[{\"CUST_NAME_FULL\":\"李莉\"}],\"cycle\":[\"0\"],\"error\":\"0\",\"countByHour_mx\":\t[{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"16\",\"YSJE\":\"0.10\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170821102550\",\"JFJE\":\"0.10\",\"BJHM\":\"13009907707\",\"JFCS\":\"1\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"72\",\"YSJE\":\"0.20\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170821150525\",\"JFJE\":\"0.20\",\"BJHM\":\"13009907707\",\"JFCS\":\"2\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"87\",\"YSJE\":\"0.20\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170822104713\",\"JFJE\":\"0.20\",\"BJHM\":\"13009907707\",\"JFCS\":\"2\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"20\",\"YSJE\":\"0.10\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170822105901\",\"JFJE\":\"0.10\",\"BJHM\":\"13009907707\",\"JFCS\":\"1\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"75\",\"YSJE\":\"0.20\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170822133935\",\"JFJE\":\"0.20\",\"BJHM\":\"13009907707\",\"JFCS\":\"2\",\"YHJE\":\"0.00\"},,{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"45\",\"YSJE\":\"0.10\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170823175811\",\"JFJE\":\"0.10\",\"BJHM\":\"13009907707\",\"JFCS\":\"1\",\"YHJE\":\"0.00\"},{\"SYLX\":\"小灵通市话详单\",\"THSC\":\"163\",\"YSJE\":\"0.30\",\"BJQY\":\"油城通被叫号段:13\",\"HSSJ\":\"20170820153942\",\"JFJE\":\"0.30\",\"BJHM\":\"13q009907707\",\"JFCS\":\"3\",\"YHJE\":\"0.00\"}],\"error_message\":\"\",\"countByHour_hj\":[{\"SYLX\":\"48\",\"THSC\":\"3719\",\"YSJE\":\"9.10\",\"HDZS\":\"48\",\"JFJE\":\"9.10\",\"JFCS\":\"91\",\"YHJE\":\"0.00\"}]}" };
    o = JSON.parse(o.obj.replace(/,{2,}/g, ','))//去掉2个连续的逗号为1个
    alert('error='+o.error)
    alert('error_message=' + o.error_message)
    for (attr in o.countByHour_hj[0]) alert(attr + '=' + o.countByHour_hj[0][attr]);
    for (var i = 0; i < o.countByHour_mx.length; i++) alert(o.countByHour_mx[i].SYLX + '\n' + o.countByHour_mx[i].THSC + '\n' + o.countByHour_mx[i].THSC/*..更多属性..*/)
    //其他属性自己取
yhlbest
yhlbest   2017.09.15 15:19

{
"obj": {
"cust_info": [
{
"CUST_NAME_FULL": "李莉"
}
],
"cycle": [
"0"
],
"error": "0",
"countByHour_mx": [
{
"SYLX": "小灵通市话详单",
"THSC": "16",
"YSJE": "0.10",
"BJQY": "油城通被叫号段:13",
"HSSJ": "20170821102550",
"JFJE": "0.10",
"BJHM": "13009907707",
"JFCS": "1",
"YHJE": "0.00"
},
{
"SYLX": "小灵通市话详单",
"THSC": "72",
"YSJE": "0.20",
"BJQY": "油城通被叫号段:13",
"HSSJ": "20170821150525",
"JFJE": "0.20",
"BJHM": "13009907707",
"JFCS": "2",
"YHJE": "0.00"
},
{
"SYLX": "小灵通市话详单",
"THSC": "87",
"YSJE": "0.20",
"BJQY": "油城通被叫号段:13",
"HSSJ": "20170822104713",
"JFJE": "0.20",
"BJHM": "13009907707",
"JFCS": "2",
"YHJE": "0.00"
},
{
"SYLX": "小灵通市话详单",
"THSC": "20",
"YSJE": "0.10",
"BJQY": "油城通被叫号段:13",
"HSSJ": "20170822105901",
"JFJE": "0.10",
"BJHM": "13009907707",
"JFCS": "1",
"YHJE": "0.00"
},
{
"SYLX": "小灵通市话详单",
"THSC": "75",
"YSJE": "0.20",
"BJQY": "油城通被叫号段:13",
"HSSJ": "20170822133935",
"JFJE": "0.20",
"BJHM": "13009907707",
"JFCS": "2",
"YHJE": "0.00"
},
{
"SYLX": "小灵通市话详单",
"THSC": "45",
"YSJE": "0.10",
"BJQY": "油城通被叫号段:13",
"HSSJ": "20170823175811",
"JFJE": "0.10",
"BJHM": "13009907707",
"JFCS": "1",
"YHJE": "0.00"
},
{
"SYLX": "小灵通市话详单",
"THSC": "163",
"YSJE": "0.30",
"BJQY": "油城通被叫号段:13",
"HSSJ": "20170820153942",
"JFJE": "0.30",
"BJHM": "13q009907707",
"JFCS": "3",
"YHJE": "0.00"
}
],
"error_message": "",
"countByHour_hj": [
{
"SYLX": "48",
"THSC": "3719",
"YSJE": "9.10",
"HDZS": "48",
"JFJE": "9.10",
"JFCS": "91",
"YHJE": "0.00"
}
]
}
}

你要循环哪个数组啊?

yhlbest
yhlbest 回复qq_25884459: 尴尬 ; 不是 json.parse 一下 就可以点出来了吗? 还是说你这个 不是个标准的json格式数据,要正则替换后才能用
2 个月之前 回复
qq_25884459
qq_25884459 回复yhlbest: 取不出来 如果能这么取出来,我何必悬赏呢?
2 个月之前 回复
yhlbest
yhlbest 回复qq_25884459: 既然是json 不是直接 obj.countByHour_mx 就可以啦? 没明白你的意思
2 个月之前 回复
qq_37524684
qq_37524684 牛B,居然有耐心整理
2 个月之前 回复
qq_25884459
qq_25884459 { "SYLX": "小灵通市话详单", "THSC": "45", "YSJE": "0.10", "BJQY": "油城通被叫号段:13", "HSSJ": "20170823175811", "JFJE": "0.10", "BJHM": "13009907707", "JFCS": "1", "YHJE": "0.00" } 这个数组
2 个月之前 回复
yhlbest
yhlbest   2017.09.15 15:24

既然是json 不是直接 obj.countByHour_mx 就可以啦?

cashT
cashT   2017.09.15 15:24
var str = '[{"name":"","num":"","price":""},{"name":"","num":"","price":""}]';
var xqo = eval('(' + str + ')');
for(var i in xqo){
    alert(xqo[i].name);
}
KiterCher
KiterCher   2017.09.15 15:35

直接给你上代码吧!

function jsonto() {
    var jsonStr = '你的json字符串'
    var json = JSON.parse(jsonStr)
    var obj = json.obj;
    var countByHour_mx = obj.countByHour_mx;
    countByHour_mx.forEach(function (index,value,array) {
        console.log(array[index].SYLX);
        //这里在遍历你的数组,需要做什么你自己定义就行
    })
}

Csdn user default icon
上传中...
上传图片
插入图片