【问题标题】:Getting a variable from a multidimentional JSON array using Jquery使用 Jquery 从多维 JSON 数组中获取变量
【发布时间】:2014-06-28 19:51:29
【问题描述】:

我正在使用 JQUERY 检索多维 JSON 数组。

我需要打印出数组中的各种项目,但我很难弄清楚如何通过数组并获取这些项目,以便我可以将它们插入 HTML。

这是一个数组示例(这是从下面引用的 jsonpage.php 中获取的。

{
   "count":1,
   "total_count":1,
   "contacts":[
      {
         "id":92840643,
         "user_id":55536,
         "first_name":"John",
         "last_name":"Doe",
         "full_name":"John  Doe",
         "initials":"JD",
         "title":null,
         "company":null,
         "email":"john@doe.com",
         "avatar":"https://graph.facebook.com/123454/picture?type=large",
         "avatar_url":"https://graph.facebook.com/123454/picture?type=large",
         "last_contacted":null,
         "visible":true,
         "twitter":null,
         "facebook_url":null,
         "linkedin_url":null,
         "first_contacted":null,
         "created_at":"2014-05-26T19:06:55Z",
         "updated_at":"2014-05-26T19:12:42Z",
         "hits":0,
         "user_bucket_id":486405,
         "team_parent_id":null,
         "snoozed_at":null,
         "snooze_days":null,
         "groupings":[
            {
               "id":21554286,
               "type":"Grouping::Location",
               "name":"Johnson, NY",
               "stub":"frisco tx",
               "bucket_id":null,
               "user_id":55536,
               "domain_id":null,
               "editable":null,
               "conversable":null,
               "locked":null,
               "derived_from_id":null
            },
            {
               "id":21553660,
               "type":"Grouping::Bucket",
               "name":"Top Customers",
               "stub":"top customers",
               "bucket_id":486405,
               "user_id":55536,
               "domain_id":null,
               "editable":null,
               "conversable":null,
               "locked":null,
               "derived_from_id":null,
               "has_followups":true,
               "num_days_to_followup":30,
               "program_id":null
            }
         ],
         "email_addresses":[
            "john@doe.com"
         ],
         "tags":[

         ],
         "contact_status":3,
         "team_last_contacted":null,
         "team_last_contacted_by":null,
         "phone_numbers":[

         ],
         "addresses":[
            {
               "_id":"538390cfcc0fb067d8000353",
               "created_at":"2014-05-26T19:06:55Z",
               "deleted_at":null,
               "extra_data":{
                  "address_city":"Johnson",
                  "address_state":"NY",
                  "address_country":"United States"
               },
               "label":"Address",
               "primary":null,
               "remote_id":null,
               "updated_at":"2014-05-26T19:06:55Z",
               "username":null,
               "value":"Johnson, NY\nUnited States"
            }
         ],
         "social_profiles":[

         ],
         "websites":[

         ],
         "custom_fields":[
            {
               "_id":"538390cfcc0fb067d8000354",
               "custom_field_id":46639,
               "deleted_at":null,
               "label":"WeeklyNews",
               "value":"YES"
            },
            {
               "_id":"538390cfcc0fb067d8000355",
               "custom_field_id":46640,
               "deleted_at":null,
               "label":"Current Credits",
               "value":"142"
            },
            {
               "_id":"538390cfcc0fb067d8000356",
               "custom_field_id":46641,
               "deleted_at":null,
               "label":"Total Purchased Amount",
               "value":"400"
            },
            {
               "_id":"538390cfcc0fb067d8000357",
               "custom_field_id":46642,
               "deleted_at":null,
               "label":"VDownloads",
               "value":"112"
            },
            {
               "_id":"538390cfcc0fb067d8000358",
               "custom_field_id":46643,
               "deleted_at":null,
               "label":"AEDownloads",
               "value":"9"
            },
            {
               "_id":"538390cfcc0fb067d8000359",
               "custom_field_id":46644,
               "deleted_at":null,
               "label":"ADownloads",
               "value":"53"
            },
            {
               "_id":"538390cfcc0fb067d800035a",
               "custom_field_id":46638,
               "deleted_at":null,
               "label":"Last Login",
               "value":"2014-05-25 23:14:19"
            },
            {
               "_id":"538390cfcc0fb067d800035b",
               "custom_field_id":46649,
               "deleted_at":null,
               "label":"Label",
               "value":"Group1"
            }
         ]
      }
   ]
}

这里是jquery成功代码:

 $.post('/jsonpage.php', post_data,  function(response) {



 });

现在,如果我输入警报(响应);在函数内,即:

 $.post('/jsonpage.php', post_data,  function(response) {

 alert(response);
      });

然后,它“确实”提醒上面列出的整个 JSON 字符串。

但是,如果我这样说:

 $.post('/jsonpage.php', post_data,  function(response) {

 alert(response.count);
      });

然后,我在警告框中得到了 UNDEFINED。我尝试了几个不同的变量来“警报”,但它们都未定义。

感谢您的帮助!

克雷格

【问题讨论】:

  • 除非您的 html 页面非常复杂并且您真的需要一切,否则 imo json 中的“东西”太多了,而 jsonpage.php 可能应该将那个巨大的 blob 缩减为更有用/更易于管理的东西/不是带宽密集型的。但是,嘿,也许你需要它!
  • 谢谢詹姆斯。我正在通过 API 检索它,你是对的。我不需要这一切,我会看看如何限制它而不是把所有东西都拿回来。感谢您的评论。
  • 感谢您的编辑和清理我的问题。我非常感谢。

标签: javascript jquery json


【解决方案1】:
response.total_count
response.contacts[0].id
response.contacts[0].groupings[0].stub

等等。

【讨论】:

  • response 是数据,不是 DOM 元素
  • 哦,对不起,错字。已编辑。
  • 这看起来不错,但是当我试图提醒我的变量时,我得到了 UNDEFINED。你能检查我上面的编辑吗?
  • 尝试在 Google Developer Tools 控制台中检查响应,即执行 console.log(response) 并展开结果。
  • 太棒了!祝你有美好的一天。
【解决方案2】:

以下是在 json 响应中使用数据的一些方法。希望对您有所帮助。

$.post('/jsonpage.php', post_data,  function(response) {

  if (!response.contacts || !response.contacts.length) {
    alert("Error loading that/those contact(s)");
    return;
  }

  for (var i=0, c; c = response.contacts[i]; i++) {
    // c is each contact, do stuff with c
    alert("That contact was created at " + c.created_at + " and last updated at " + c.updated_at);

    var cities = [];

    for (var j=0, a; a = c.addresses[j]; j++) {
      // a refers to each address
      cities.push(a.extra_data.address_city);
    }
    alert(c.full_name + " lives in " + cities.join(" and ") + ".");

    for (var j=0, cf; cf = c.custom_fields[j]; j++) {
      // cf is each custom_field
      // build a form or something
      // element label is cf.label
      // element value is currently cf.value
      var p = document.createElement("p");
      p.appendChild(document.createTextNode(cf.label));

      var el = document.createElement("input");
      el.type = "text";
      el.value = cf.value;

      p.appendChild(el);

      document.getElementById("someForm").appendChild(p);

    }

  }

});

【讨论】:

  • James 现在检查一下,但感谢您抽出宝贵时间发布此额外信息。/详细信息。太棒了!
【解决方案3】:

试试这个

var data = { "count": 1, "total_count": 1, "contacts": [{ "id": 92840643, "user_id": 55536, "first_name": "John", "last_name": "Doe", "full_name": "John  Doe", "initials": "JD", "title": null, "company": null, "email": "john@doe.com", "avatar": "https://graph.facebook.com/123454/picture?type=large", "avatar_url": "https://graph.facebook.com/123454/picture?type=large", "last_contacted": null, "visible": true, "twitter": null, "facebook_url": null, "linkedin_url": null, "first_contacted": null, "created_at": "2014-05-26T19:06:55Z", "updated_at": "2014-05-26T19:12:42Z", "hits": 0, "user_bucket_id": 486405, "team_parent_id": null, "snoozed_at": null, "snooze_days": null, "groupings": [{ "id": 21554286, "type": "Grouping::Location", "name": "Johnson, NY", "stub": "frisco tx", "bucket_id": null, "user_id": 55536, "domain_id": null, "editable": null, "conversable": null, "locked": null, "derived_from_id": null }, { "id": 21553660, "type": "Grouping::Bucket", "name": "Top Customers", "stub": "top customers", "bucket_id": 486405, "user_id": 55536, "domain_id": null, "editable": null, "conversable": null, "locked": null, "derived_from_id": null, "has_followups": true, "num_days_to_followup": 30, "program_id": null}], "email_addresses": ["john@doe.com"], "tags": [], "contact_status": 3, "team_last_contacted": null, "team_last_contacted_by": null, "phone_numbers": [], "addresses": [{ "_id": "538390cfcc0fb067d8000353", "created_at": "2014-05-26T19:06:55Z", "deleted_at": null, "extra_data": { "address_city": "Johnson", "address_state": "NY", "address_country": "United States" }, "label": "Address", "primary": null, "remote_id": null, "updated_at": "2014-05-26T19:06:55Z", "username": null, "value": "Johnson, NY\nUnited States"}], "social_profiles": [], "websites": [], "custom_fields": [{ "_id": "538390cfcc0fb067d8000354", "custom_field_id": 46639, "deleted_at": null, "label": "WeeklyNews", "value": "YES" }, { "_id": "538390cfcc0fb067d8000355", "custom_field_id": 46640, "deleted_at": null, "label": "Current Credits", "value": "142" }, { "_id": "538390cfcc0fb067d8000356", "custom_field_id": 46641, "deleted_at": null, "label": "Total Purchased Amount", "value": "400" }, { "_id": "538390cfcc0fb067d8000357", "custom_field_id": 46642, "deleted_at": null, "label": "VDownloads", "value": "112" }, { "_id": "538390cfcc0fb067d8000358", "custom_field_id": 46643, "deleted_at": null, "label": "AEDownloads", "value": "9" }, { "_id": "538390cfcc0fb067d8000359", "custom_field_id": 46644, "deleted_at": null, "label": "ADownloads", "value": "53" }, { "_id": "538390cfcc0fb067d800035a", "custom_field_id": 46638, "deleted_at": null, "label": "Last Login", "value": "2014-05-25 23:14:19" }, { "_id": "538390cfcc0fb067d800035b", "custom_field_id": 46649, "deleted_at": null, "label": "Label", "value": "Group1"}]}] };
    alert(data["contacts"][0]["id"]);

//get count
alert(data["count"]); //1
//get first contacts data
data["contacts"][0]["id"] //retruns 92840643
//do same for others to get data

【讨论】:

  • 我认为你有一个错字:alert(j["contacts"][0]["id"]);
猜你喜欢
  • 2016-02-12
  • 2018-11-21
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
相关资源
最近更新 更多