JQuery getJSON每个

时间:2015-09-01 05:46:52

标签: jquery json

使用 json 选择器工具,我可以找到我想要的选择......

['result'][0]['creator’][‘box_id']

但我不确定如何编写 JQuery 以获取所有 box_id

这是我的尝试,但它失败了

$.each(data.result[0]creator.box_id, function(key,value){

这是有效的

$.each(data.result[0], function(key,value){

所以我认为它是如何进入creator.box_id部分的?

提前致谢!

JSON

{
"result": [
    {
        "box_id": "d20fbaa60f1d4b4dbd8263e430286f5c",
        "start_at": 1441072565094,
        "current_at": 1441078127720
    },
    {

        "box_id": "33bb7b1f359c429ab07b677ae78dfc00",
        "start_at": 1441076404685,
        "current_at": 1441078127748,

    },

等等

我尝试添加..

$.each(data.result[0].creator.box_id, function(key,value){

我在控制台中收到一个新错误...

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in         dadfd1f55453441cb059710ece0d1a6bs 
@ jquery.js:2n.extend.each 
@ jquery.js:2(anonymous function) 
@ content.js:34n.Callbacks.j 
@ jquery.js:2n.Callbacks.k.fireWith 
@ jquery.js:2x 
@ jquery.js:4n.ajaxTransport.k.cors.a.crossDomain.send.b @ jquery.js:4

2 个答案:

答案 0 :(得分:0)

我认为你错过了点,试试这个data.result[0].creator.box_id

<强>更新

&#13;
&#13;
var data = {
"result": [
    {
        "box_id": "d20fbaa60f1d4b4dbd8263e430286f5c",
        "start_at": 1441072565094,
        "current_at": 1441078127720
    },
    {

        "box_id": "33bb7b1f359c429ab07b677ae78dfc00",
        "start_at": 1441076404685,
        "current_at": 1441078127748,

    }
]};

$.each(data.result, function(key, val) {
  console.log(val.box_id);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

以下是完整的解释:

如果你考虑低于JSON

var x = "{"code":200,"result":[{"box_id":"d20fbaa60f1d4b4dbd8263e430286f5c","changedAt":1439575955773}]}";
var y = JSON.parse(x);
  

y [“result”]是你的对象数组,现在用JQuery迭代它

$.each(y["result"],function(key,value){
                            alert("index"+key);
                            alert("box_id"+value["box_id"]);
                            alert("changedAt"+value["changedAt"])});

希望它对你来说很明确,也很有帮助。