【问题标题】:How to populate Grid with ajax where data coming from REST API JSON format?如何使用 ajax 填充来自 REST API JSON 格式的数据的网格?
【发布时间】:2020-04-02 00:13:56
【问题描述】:

我遇到了一个问题,我从后端获取了 JSON 格式的数据 格式,我无法将数据提取到表中。

>JSON格式:

    {
    "success": true,
    "code": "200",
    "message": "successfull",
    "response": {
        "A00XYZ": [
            {
                "9": "30.00",
                "26": "43.01",
                "36": "632.56",
                "39": "56.82",
                "58": "13.12",
                "65": "292.73",
                "148": "0.00",
                "393": "1.96",
                "472": "64.17",
                "517": "0.00",
                "firstName": "jhon",
                "lastName": "ends"
            }
        ],
        "A00XYZ2": [
            {
                "9": "21.26",
                "26": "78.19",
                "36": "1003.91",
                "39": "65.73",
                "58": "78.65",
                "65": "23.00",
                "148": "0.00",
                "393": "2.40",
                "472": "55.84",
                "517": "34",
                "firstName": "Dummy",
                "lastName": "Knight"
            }
        ]
}

我尝试了几种方法。仍然存在一些问题。

> jQuery:

if(data.response == 200){
    for (var i = 0; i < data.response.length; i++) {
    for (var key in data.response[i]) {
        for (var j = 0; j < data.response[i][key].length; j++) {
            console.log(data.response[i][key][j])
        }
    }
  }
}

【问题讨论】:

    标签: arrays json ajax rest datatable


    【解决方案1】:

    我认为您错过的是您需要获取data.response[i] 的第一个元素。

    在这里试试,这是你想要的吗?

        var result = {}
        $.each(data.response, function(key) {
          result[key] = {}
          $.each(data.response[key][0], function(e) {
            result[key][e] = data.response[key][0][e];
          })
        })
    

    var data = {
            "success": true,
            "code": "200",
            "message": "successfull",
            "response": {
                "A00XYZ": [
                    {
                        "9": "30.00",
                        "26": "43.01",
                        "36": "632.56",
                        "39": "56.82",
                        "58": "13.12",
                        "65": "292.73",
                        "148": "0.00",
                        "393": "1.96",
                        "472": "64.17",
                        "517": "0.00",
                        "firstName": "jhon",
                        "lastName": "ends"
                    }
                ],
                "A00XYZ2": [
                    {
                        "9": "21.26",
                        "26": "78.19",
                        "36": "1003.91",
                        "39": "65.73",
                        "58": "78.65",
                        "65": "23.00",
                        "148": "0.00",
                        "393": "2.40",
                        "472": "55.84",
                        "517": "34",
                        "firstName": "Dummy",
                        "lastName": "Knight"
                    }
                ]
             }
        };
        
        var response = data.response;
        var result = {}
        $.each(response, function(key) {
          result[key] = {}
          $.each(response[key][0], function(e) {
            result[key][e] = response[key][0][e];
          })
        })
        console.log(result)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      相关资源
      最近更新 更多