【问题标题】:Looping through a json object in javascript在javascript中循环一个json对象
【发布时间】:2013-12-12 05:43:19
【问题描述】:

我正在尝试查看一个对象,但基于结构我不知道如何获取数据。

对象:

{
"177":{
     "text":"test text",
     "user_name":"Admin",
     "date":"1385494358",
     "docs":{
        "document_name": [
            "marketing_service",
            "maintenance_service",
            "development_service"],
        "document_type":[
            "png",
            "png",
            "png"]
       }
},
"174":{
     "text":"Some more images",
     "user_name":"Admin",
     "date":"1385493618",
     "docs":{
        "document_name": [
            "marketing_service16",
            "maintenance_service53"],
     "document_type":[
            "png","png"]
      }
}
}

我在 jQuery 中尝试的循环

var obj = $.parseJSON(data);
$(obj).each(function(index, note) {
    console.log(note.text + note.user_name + note.date_created);
});

它返回未定义。我做错了什么?

【问题讨论】:

  • 什么是data 是字符串吗?
  • data是上面代码中显示的对象
  • 请准确说明您如何声明和设置data。如果data 已经是您不想使用$.parseJSON() 的对象。

标签: javascript jquery json loops foreach


【解决方案1】:

要遍历 json 对象,只需使用 for 循环。

你这样做的方式会返回错误,因为它试图从不存在的页面中选择那个 json 对象。

示例代码:

var obj = $.parseJSON(data); //assuming `data` is a string
for(index in obj) {
    var note = obj[index];
    console.log(note.text + note.user_name + note.date_created);
};

【讨论】:

  • 你应该非常小心地使用 for in 而不首先检查对象是否具有属性。
  • @DavidBarker 是什么意思?
  • “它正在尝试从页面中选择那个 json 对象” - No it isn't.
  • @qwertnl obj.prototype 是对象的属性,obj.length 也是如此。使用 for in 循环时,您需要在尝试从中收集数据之前检查 if (object.hasOwnProperty(property))
  • 使用.hasOwnProperty 只能在有意义的情况下使用。将它用作每个对象的每个循环的广泛保护是没有意义的。
【解决方案2】:

这样做会更好一点:

$.each(obj, function ( index, note ) {
    console.log( note.text + note.user_name + note.date_created );
});

这应该可以满足您的需要

【讨论】:

    【解决方案3】:

    工作演示: http://jsfiddle.net/gZ7pd/ for document http://jsfiddle.net/NGqfB/

    问题是 Json 对象上的parseJson

    在上面的演示中,var obj = $.parseJSON(data); 返回 null,如果我使用数据,它将起作用。

    希望这有助于;)

    代码

    var data = {
    "177":{
         "text":"test text",
         "user_name":"Admin",
         "date":"1385494358",
         "docs":{
            "document_name": [
                "marketing_service",
                "maintenance_service",
                "development_service"],
            "document_type":[
                "png",
                "png",
                "png"]
           }
    },
    "174":{
         "text":"Some more images",
         "user_name":"Admin",
         "date":"1385493618",
         "docs":{
            "document_name": [
                "marketing_service16",
                "maintenance_service53"],
         "document_type":[
                "png","png"]
          }
    }
    };
    
    var obj = $.parseJSON(data);
    alert(obj)
    
    $.each(data,function(index, note) {
    
        alert(note.text + note.user_name + note.date_created);
    });
    

    【讨论】:

    • @Downvoter 关心回复为什么投反对票? :) 还是只是个人选择否决?没有建设性!
    • @kkemple 谢谢bruvoo :) 有些人无缘无故投反对票,这非常可悲!干杯哟! +1 给你!
    【解决方案4】:

    这样试试

    var obj = $.parseJSON(data);
    for(var n in obj){
     var note = obj[n]
     console.log(note.text + note.user_name + note.date_created);
    }
    

    【讨论】:

    • 基本上就是我所说的:-P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多