2015-02-10 70 views
-1

我使用下面的命令拉着一個JSON數組:jQuery的JSON獲得訪問結果

$.get('URL') 
.always(function(data) { 
    console.log(data); 

時直接訪問的URL返回以下信息:

{ 
    "user": { 
     "ipaddr": "192.168.37.10.", 
     "system_date": "Feb 10 10:38:51 PST 2015" 
    }, 
    "clean_graph_allow": 0, 
    "client": { 
     "account_information": { 
      0: { 
       "ID": 7611, 
       "accountID": "abcd", 
       "label": "Billing", 
       "email": "[email protected]", 
       "firstName": "Bob", 
       "lastName": "Jones", 
       "company": "Bob inc.", 
       "address": "123 Fake Street", 
       "city": "Nowhere", 
       "province": "CA", 
       "postalCode": 91111, 
       "country": "US", 
       "phone": 5555555555, 
       "notes": null, 
       "company_type": null 
      }, 
      1: { 
       "ID": 7611, 
       "accountID": "abcd", 
       "label": "Billing", 
       "email": "[email protected]", 
       "firstName": "Bob", 
       "lastName": "Jones", 
       "company": "Bob inc.", 
       "address": "123 Fake Street", 
       "city": "Nowhere", 
       "province": "CA", 
       "postalCode": 91111, 
       "country": "US", 
       "phone": 5555555555, 
       "notes": null, 
       "company_type": null 
      }, 
      2: { 
       "ID": 7611, 
       "accountID": "abcd", 
       "label": "Billing", 
       "email": "[email protected]", 
       "firstName": "Bob", 
       "lastName": "Jones", 
       "company": "Bob inc.", 
       "address": "123 Fake Street", 
       "city": "Nowhere", 
       "province": "CA", 
       "postalCode": 91111, 
       "country": "US", 
       "phone": 5555555555, 
       "notes": null, 
       "company_type": null 
      }, 
      "contact": { 
       "ID": 7611, 
       "accountID": "abcd", 
       "label": "Billing", 
       "email": "[email protected]", 
       "firstName": "Bob", 
       "lastName": "Jones", 
       "company": "Bob inc.", 
       "address": "123 Fake Street", 
       "city": "Nowhere", 
       "province": "CA", 
       "postalCode": 91111, 
       "country": "US", 
       "phone": 5555555555, 
       "notes": null, 
       "company_type": null 
      } 
     }, 
     "is_reseller": 300 
    }, 
    "hidden_pages": [], 
    "action": "dashboard_api_stats", 
    "attacks_today": 4920, 
    "blocks": 1816, 
    "short_blocks": 101, 
    "nullroutes": 314, 
    "num_countries": 0, 
    "attack_dnsc": 3, 
    "attack_pstc": 6, 
    "attack_gc": 28, 
    "attack_core": 55, 
    "attack_tcpc": 8, 
    "attack_udpc": 0, 
    "regional": { 
     "": 100 
    } 
} 

我回來從響應$ .get或$ .getJSON命令如下所示:

Object { 
    readyState: 4, 
    getResponseHeader: function, 
     getAllResponseHeaders: function, 
      setRequestHeader: function, 
       overrideMimeType: function… 
      } 
      abort: function(a) { 
       var b = a || u; 
       return i && i.abort(b), x(0, b), this 
      } 
      always: function() { 
       return e.done(arguments).fail(arguments), this 
      } 
      complete: function() { 
       if (h) { 
        var d = h.length; 
        ! function f(b) { 
         m.each(b, function(b, c) { 
          var d = m.type(c); 
          "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c) 
         }) 
        }(arguments), b ? e = h.length : c && (g = d, j(c)) 
       } 
       return this 
      } 
      done: function() { 
       if (h) { 
        var d = h.length; 
        ! function f(b) { 
         m.each(b, function(b, c) { 
          var d = m.type(c); 
          "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c) 
         }) 
        }(arguments), b ? e = h.length : c && (g = d, j(c)) 
       } 
       return this 
      } 
      error: function() { 
       if (h) { 
        var d = h.length; 
        ! function f(b) { 
         m.each(b, function(b, c) { 
          var d = m.type(c); 
          "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c) 
         }) 
        }(arguments), b ? e = h.length : c && (g = d, j(c)) 
       } 
       return this 
      } 
      fail: function() { 
       if (h) { 
        var d = h.length; 
        ! function f(b) { 
         m.each(b, function(b, c) { 
          var d = m.type(c); 
          "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c) 
         }) 
        }(arguments), b ? e = h.length : c && (g = d, j(c)) 
       } 
       return this 
      } 
      getAllResponseHeaders: function() { 
       return 2 === t ? f : null 
      } 
      getResponseHeader: function(a) { 
       var b; 
       if (2 === t) { 
        if (!j) { 
         j = {}; 
         while (b = Cc.exec(f)) j[b[1].toLowerCase()] = b[2] 
        } 
        b = j[a.toLowerCase()] 
       } 
       return null == b ? null : b 
      } 
      overrideMimeType: function(a) { 
       return t || (k.mimeType = a), this 
      } 
      pipe: function() { 
       var a = arguments; 
       return m.Deferred(function(c) { 
        m.each(b, function(b, f) { 
         var g = m.isFunction(a[b]) && a[b]; 
         e[f[1]](function() { 
          var a = g && g.apply(this, arguments); 
          a && m.isFunction(a.promise) ? a.promise().done(c.resolve).fail(c.reject).progress(c.notify) : c[f[0] + "With"](this === d ? c.promise() : this, g ? [a] : arguments) 
         }) 
        }), a = null 
       }).promise() 
      } 
      progress: function() { 
       if (h) { 
        var d = h.length; 
        ! function f(b) { 
         m.each(b, function(b, c) { 
          var d = m.type(c); 
          "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c) 
         }) 
        }(arguments), b ? e = h.length : c && (g = d, j(c)) 
       } 
       return this 
      } 
      promise: function(a) { 
       return null != a ? m.extend(a, d) : d 
      } 
      readyState: 4 responseText: "{" 
      user ":{" 
      ipaddr ":" 
      192.168.37.10. 
      "," 
      system_date ":" 
      Feb 10 10: 38: 51 PST 2015 "}," 
      clean_graph_allow ":0," 
      client ":{" 
      account_information ":{0:{" 
      ID ":7611," 
      accountID ":" 
      abcd "," 
      label ":" 
      Billing "," 
      email ":" 
      [email protected] jones.com "," 
      firstName ":" 
      Bob "," 
      lastName ":" 
      Jones "," 
      company ":" 
      Bob inc. 
      "," 
      address ":" 
      123 Fake Street "," 
      city ":" 
      Nowhere "," 
      province ":" 
      CA "," 
      postalCode ":91111," 
      country ":" 
      US "," 
      phone ":5555555555," 
      notes ":null," 
      company_type ":null},1:{" 
      ID ":7611," 
      accountID ":" 
      abcd "," 
      label ":" 
      Billing "," 
      email ":" 
      [email protected] jones.com "," 
      firstName ":" 
      Bob "," 
      lastName ":" 
      Jones "," 
      company ":" 
      Bob inc. 
      "," 
      address ":" 
      123 Fake Street "," 
      city ":" 
      Nowhere "," 
      province ":" 
      CA "," 
      postalCode ":91111," 
      country ":" 
      US "," 
      phone ":5555555555," 
      notes ":null," 
      company_type ":null},2:{" 
      ID ":7611," 
      accountID ":" 
      abcd "," 
      label ":" 
      Billing "," 
      email ":" 
      [email protected] jones.com "," 
      firstName ":" 
      Bob "," 
      lastName ":" 
      Jones "," 
      company ":" 
      Bob inc. 
      "," 
      address ":" 
      123 Fake Street "," 
      city ":" 
      Nowhere "," 
      province ":" 
      CA "," 
      postalCode ":91111," 
      country ":" 
      US "," 
      phone ":5555555555," 
      notes ":null," 
      company_type ":null}," 
      contact ":{" 
      ID ":7611," 
      accountID ":" 
      abcd "," 
      label ":" 
      Billing "," 
      email ":" 
      [email protected] jones.com "," 
      firstName ":" 
      Bob "," 
      lastName ":" 
      Jones "," 
      company ":" 
      Bob inc. 
      "," 
      address ":" 
      123 Fake Street "," 
      city ":" 
      Nowhere "," 
      province ":" 
      CA "," 
      postalCode ":91111," 
      country ":" 
      US "," 
      phone ":5555555555," 
      notes ":null," 
      company_type ":null}}," 
      is_reseller ":300}," 
      hidden_pages ":[]," 
      action ":" 
      dashboard_api_stats "," 
      attacks_today ":4920," 
      blocks ":1816," 
      short_blocks ":101," 
      nullroutes ":314," 
      num_countries ":0," 
      attack_dnsc ":3," 
      attack_pstc ":6," 
      attack_gc ":28," 
      attack_core ":55," 
      attack_tcpc ":8," 
      attack_udpc ":0," 
      regional ":{" 
      ":100}}" 
      setRequestHeader: function(a, b) { 
       var c = a.toLowerCase(); 
       return t || (a = s[c] = s[c] || a, r[a] = b), this 
      } 
      state: function() { 
       return c 
      } 
      status: 200 statusCode: function(a) { 
       var b; 
       if (a) 
        if (2 > t) 
         for (b in a) q[b] = [q[b], a[b]]; 
        else v.always(a[v.status]); 
       return this 
      } 
      statusText: "OK" 
      success: function() { 
       if (h) { 
        var d = h.length; 
        ! function f(b) { 
         m.each(b, function(b, c) { 
          var d = m.type(c); 
          "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c) 
         }) 
        }(arguments), b ? e = h.length : c && (g = d, j(c)) 
       } 
       return this 
      } 
      then: function() { 
       var a = arguments; 
       return m.Deferred(function(c) { 
        m.each(b, function(b, f) { 
         var g = m.isFunction(a[b]) && a[b]; 
         e[f[1]](function() { 
          var a = g && g.apply(this, arguments); 
          a && m.isFunction(a.promise) ? a.promise().done(c.resolve).fail(c.reject).progress(c.notify) : c[f[0] + "With"](this === d ? c.promise() : this, g ? [a] : arguments) 
         }) 
        }), a = null 
       }).promise() 
      } 
      __proto__: Object 

對於上面的格式字符串我很抱歉G。我不知道如何正確格式化。

無論如何,那個重要的部分是responseText。 responseText等同於直接訪問URL時返回的數據。我需要能夠從中抽取特定元素(例如,attack_today)。

一些東西:

data.responseText.attacks_today 
OR 
data['responseText']['attacks_today'] 

不要工作。我也嘗試循環的各種方法,但他們都給予我同樣類型的錯誤:在

var arr = data.responseText;          
$.each(arr, function(index, entry) { 

結果:

Uncaught TypeError: Cannot use 'in' operator to search for '1700' in ... 

我們有了可以訪問JSON中的元素一些簡單的辦法,除非我我錯了,但我只是沒有看到它。

我正在使用jQuery版本1.11.0。

謝謝。

+0

爲什麼downvote? – Madbreaks 2015-02-10 18:52:23

+1

使用'$ .getJSON()'。 – 2015-02-10 18:52:50

+0

Matt:在我測試過的每一種情況下(無論是直接訪問數組元素,還是遍歷json數組),get和getJSON都有同樣的問題。 – user2171332 2015-02-10 18:55:05

回答

1

您的Ajax失敗。文檔說,如果請求成功,第一個參數將是xhr對象或數據對象。

jqXHR.always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { });

An alternative construct to the complete callback option, the .always() method replaces the deprecated .complete() method.

In response to a successful request, the function's arguments are the same as those of .done(): data, textStatus, and the jqXHR object. For failed requests the arguments are the same as those of .fail(): the jqXHR object, textStatus, and errorThrown. Refer to deferred.always() for implementation details.

http://api.jquery.com/jquery.ajax/

你所看到的物體是XHR對象,因爲請求失敗。如果您想確定始終只是在找出爲什麼您的請求失敗後纔得到結果,請使用.done(數據)。

+0

保羅,是的,我實際上已經意識到它幾分鐘前a麥克特向我指出了驗證JSON的方向。 .always的作品,但.done沒有。 .fail會這樣做,它會返回一些不好的數據。我會深入分析爲什麼數據不好。謝謝。 – user2171332 2015-02-10 19:29:16

+0

確保你在應答頭中返回application/json。如果您傳遞給請求的URL是正確的,並且您在瀏覽器中看到了良好的響應,那麼我可以想到的一件事就是將其分解。 – 2015-02-10 19:33:01

+0

響應頭是正確的。問題是,在代碼中的其他地方,與我的工作空間完全分離,一些額外的數據被傳遞到json返回。抑制額外的東西解決了JSON驗證問題,解決了問題。謝謝! – user2171332 2015-02-10 19:44:21