zjyuan1100 2015-01-09 07:48 采纳率: 36.4%
浏览 2004
已采纳

jquery 处理json数据问题!急!在线等!

var json={"Table1":[{"XMID":"B0","ysdate":"2015-01-01T00:00:00","SZH":7373.0},{"XMID":"B0","ysdate":"2015-01-02T00:00:00","SZH":7845.0},{"XMID":"C0","ysdate":"2015-01-01T00:00:00","SZH":327.0},{"XMID":"C0","ysdate":"2015-01-02T00:00:00","SZH":295.0},{"XMID":"D","ysdate":"2015-01-01T00:00:00","SZH":211.0},{"XMID":"D","ysdate":"2015-01-02T00:00:00","SZH":254.0},{"XMID":"K","ysdate":"2015-01-01T00:00:00","SZH":7911.0},{"XMID":"K","ysdate":"2015-01-02T00:00:00","SZH":8394.0}]}

最终想要js输出表格:
ysdate B0 C0 D K
2015-01-01 7373.0 327.0 211.0 7911.0
2015-01-02 7845.0 295.0 254.0 9394.0

  • 写回答

7条回答 默认 最新

  • 斯洛文尼亚旅游 2015-01-09 08:39
    关注

    http://ask.csdn.net/questions/161313

    发了2个贴?发个自动标题的给你,更新了下代码

    
        var json = { "Table1": [
        { "XMID": "B0", "ysdate": "2015-01-01T00:00:00", "SZH": 7373.0 },
        { "XMID": "B0", "ysdate": "2015-01-02T00:00:00", "SZH": 7845.0 },
        { "XMID": "C0", "ysdate": "2015-01-01T00:00:00", "SZH": 327.0 },
        { "XMID": "C0", "ysdate": "2015-01-02T00:00:00", "SZH": 295.0 },
        { "XMID": "D", "ysdate": "2015-01-01T00:00:00", "SZH": 211.0 },
        { "XMID": "D", "ysdate": "2015-01-02T00:00:00", "SZH": 254.0 },
        { "XMID": "K", "ysdate": "2015-01-01T00:00:00", "SZH": 7911.0 },
        { "XMID": "K", "ysdate": "2015-01-02T00:00:00", "SZH": 8394.0}]
        };
        var rows = json.Table1,item;
        var result = {},ritem,keys=',',key;
        for (var i = 0, j = rows.length; i < j; i++) {
            item = rows[i];
            ritem = result[item.ysdate] || {};
            key = item.XMID;
            if (keys.indexOf(',' + key + ',') == -1) keys += key + ',';
            if (ritem[key]) ritem[key] += ',' + item.SZH;
            else ritem[key] = item.SZH;
            result[item.ysdate] = ritem;
        }
        var s = '<table border="1"><tr><th>ysdate</th>';
        var arr = keys.replace(/^,|,$/g,'').split(',');
        for (var i = 0; i < arr.length; i++) s += '<th>'+arr[i]+'</th>';
        s += '</tr>';
        for (k in result) {
            s += '<tr><td>' + (k.split('T')[0]) + '</td>'
            for (var i = 0; i < arr.length; i++) s += '<td>' + (result[k][arr[i]] || '') + '</td>';
        }
        s += '</table>';
        document.write(s);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?