1. 程式人生 > >多重json字串解析

多重json字串解析

折騰了一天,才算是搞定這個簡單的問題。

資料來源:

[{"total":"2","list":[{"Fiber_Core_ID":"2","Fiber_ID":"1 ","Spectrum":"1-2 ","ServiceType":"收費 ","Remark":null,"SortCode":null},
{"Fiber_Core_ID":"13","Fiber_ID":"1 ","Spectrum":"2-5 ","ServiceType":"備用 ","Remark":null,"SortCode":null},
{"Fiber_Core_ID":"12","Fiber_ID":"1 ","Spectrum":"2-4 ","ServiceType":"監控 ","Remark":null,"SortCode":null},
{"Fiber_Core_ID":"11","Fiber_ID":"1 ","Spectrum":"2-3 ","ServiceType":"監控 ","Remark":null,"SortCode":null},
{"Fiber_Core_ID":"10","Fiber_ID":"1 ","Spectrum":"2-2 ","ServiceType":"監控 ","Remark":null,"SortCode":null}]}]

前端AJAX解析:
function CoreInfo(obj) {
    $.ajax({
        type: 'get',//請求方式
        dataType: 'text',//設定返回資料的格式
        url: '/fiber/getcore',//請求地址
        data: { fiberID: obj, pageSize: pagesize, currentPage: currentpage },//請求引數
        success: function (jss) {
            var jsonData = eval("(" + jss + ")");
            var totalpage = jsonData[0].total;
            $("#fiber_core").empty();
            $.each(jsonData[0].list,function(index,val){
                $("#fiber_core").append("<tr><td>" + val.Spectrum + "</td><td>" + val.ServiceType + "</td><td>" + val.Remark + "</td></tr>");
            });
            //載入分頁工具
            $('#pageList').twbsPagination({
                totalPages: totalpage,
                visiblePages: 5,
                version: '1.1'
            });
        },
        error: function (msg) {
            alert("沒有資料");
        }
    });
    $("#fiber_name").empty();
    var fi = $("#"+obj).text();
    $("#fiber_name").append(fi);
}

關鍵是回傳的資料為object格式,需要前端用eval()進行格式化。

記錄一下,明天接著處理資料。