在数组中解析JSON数组并使用键值

时间:2016-04-23 18:35:14

标签: jquery arrays json

我通过AJAX获取此JSON数组:

myplugin.dll

我希望得到的结果看起来像这样(在控制台中):

{
    "success": 1,
    "message": {
        "ar": {
            "total": 747.4851,
            "list": [{
                "total_statements": 1,
                "total_due": 747.4851,
                "name": "Paul McBilling",
                "connection_id": 154
            }]
        },
        "ap": {
            "total": 0,
            "list": []
        },
        "graphs": {
            "graph": {
                "2016-01": 13,
                "2016-02": 0,
                "2016-03": 0,
                "2016-04": 747.4851,
                "2016-05": 0,
                "2016-06": 0,
                "2016-07": 0,
                "2016-08": 0,
                "2016-09": 0,
                "2016-10": 0,
                "2016-11": 0,
                "2016-12": 0
            }
        },
        "cached": 1
    }
}

我正在尝试记录内部"图表的每个键和值对。阵列。

我试过这个:

2016-01: 13
2016-02: 0
... 

但这回归的只有一次:

$(data.message.graphs.graph).each(function(key, value){
    console.log(key +' and '+ value);
});

2 个答案:

答案 0 :(得分:3)

您需要使用 jQuery.each() 来迭代对象

var data = {
  "success": 1,
  "message": {
    "ar": {
      "total": 747.4851,
      "list": [{
        "total_statements": 1,
        "total_due": 747.4851,
        "name": "Paul McBilling",
        "connection_id": 154
      }]
    },
    "ap": {
      "total": 0,
      "list": []
    },
    "graphs": {
      "graph": {
        "2016-01": 13,
        "2016-02": 0,
        "2016-03": 0,
        "2016-04": 747.4851,
        "2016-05": 0,
        "2016-06": 0,
        "2016-07": 0,
        "2016-08": 0,
        "2016-09": 0,
        "2016-10": 0,
        "2016-11": 0,
        "2016-12": 0
      }
    },
    "cached": 1
  }
}

$.each(data.message.graphs.graph,function(key, value) {
  console.log(key + ' and ' + value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

或者在纯JavaScript中使用 Object.keys() 获取密钥数组,使用 forEach() 进行迭代。

var data = {
  "success": 1,
  "message": {
    "ar": {
      "total": 747.4851,
      "list": [{
        "total_statements": 1,
        "total_due": 747.4851,
        "name": "Paul McBilling",
        "connection_id": 154
      }]
    },
    "ap": {
      "total": 0,
      "list": []
    },
    "graphs": {
      "graph": {
        "2016-01": 13,
        "2016-02": 0,
        "2016-03": 0,
        "2016-04": 747.4851,
        "2016-05": 0,
        "2016-06": 0,
        "2016-07": 0,
        "2016-08": 0,
        "2016-09": 0,
        "2016-10": 0,
        "2016-11": 0,
        "2016-12": 0
      }
    },
    "cached": 1
  }
}

Object.keys(data.message.graphs.graph).forEach(function(k) {
  console.log(k + ' and ' + data.message.graphs.graph[k]);
});

甚至您可以使用 for...in 循环

var data = {
  "success": 1,
  "message": {
    "ar": {
      "total": 747.4851,
      "list": [{
        "total_statements": 1,
        "total_due": 747.4851,
        "name": "Paul McBilling",
        "connection_id": 154
      }]
    },
    "ap": {
      "total": 0,
      "list": []
    },
    "graphs": {
      "graph": {
        "2016-01": 13,
        "2016-02": 0,
        "2016-03": 0,
        "2016-04": 747.4851,
        "2016-05": 0,
        "2016-06": 0,
        "2016-07": 0,
        "2016-08": 0,
        "2016-09": 0,
        "2016-10": 0,
        "2016-11": 0,
        "2016-12": 0
      }
    },
    "cached": 1
  }
}
for (var k in data.message.graphs.graph) {
  if (data.message.graphs.graph.hasOwnProperty(k)) {
    console.log(k + ' and ' + data.message.graphs.graph[k]);
  }
}

答案 1 :(得分:2)

您可以使用javascript for,如下所示。

var data = {
  "success": 1,
  "message": {
    "ar": {
      "total": 747.4851,
      "list": [{
        "total_statements": 1,
        "total_due": 747.4851,
        "name": "Paul McBilling",
        "connection_id": 154
      }]
    },
    "ap": {
      "total": 0,
      "list": []
    },
    "graphs": {
      "graph": {
        "2016-01": 13,
        "2016-02": 0,
        "2016-03": 0,
        "2016-04": 747.4851,
        "2016-05": 0,
        "2016-06": 0,
        "2016-07": 0,
        "2016-08": 0,
        "2016-09": 0,
        "2016-10": 0,
        "2016-11": 0,
        "2016-12": 0
      }
    },
    "cached": 1
  }
}

var graph = data.message.graphs.graph;
for (var key in graph) {
  console.log(key + ' and ' + graph[key]);
}