2015-09-25 17 views
0

這是我的JSON代碼。我將這個json存儲在一個數組中。如何使用Javascript讀取JSON數組以獲取值(如果存在)

{ 
"kind": "urlshortener#url", 
"id": "http://goo.gl/2FIrtF", 
"longUrl": "http://hike.com/?utm_source=facebook", 
"status": "OK", 
"created": "2015-09-22T13:45:53.645+00:00", 
"analytics": { 
    "allTime": { 
     "shortUrlClicks": "1", 
     "longUrlClicks": "1", 
     "referrers": [ 
      { 
       "count": "1", 
       "id": "unknown" 
      } 
     ], 
     "countries": [ 
      { 
       "count": "1", 
       "id": "IN" 
      } 
     ], 
     "browsers": [ 
      { 
       "count": "1", 
       "id": "Chrome" 
      } 
     ], 
     "platforms": [ 
      { 
       "count": "1", 
       "id": "Macintosh" 
      } 
     ] 
    }, 
    "month": { 
     "shortUrlClicks": "1", 
     "longUrlClicks": "1", 
     "referrers": [ 
      { 
       "count": "1", 
       "id": "unknown" 
      } 
     ], 
     "countries": [ 
      { 
       "count": "1", 
       "id": "IN" 
      } 
     ], 
     "browsers": [ 
      { 
       "count": "1", 
       "id": "Chrome" 
      } 
     ], 
     "platforms": [ 
      { 
       "count": "1", 
       "id": "Macintosh" 
      } 
     ] 
    }, 
    "week": { 
     "shortUrlClicks": "1", 
     "longUrlClicks": "1", 
     "referrers": [ 
      { 
       "count": "1", 
       "id": "unknown" 
      } 
     ], 
     "countries": [ 
      { 
       "count": "1", 
       "id": "IN" 
      } 
     ], 
     "browsers": [ 
      { 
       "count": "1", 
       "id": "Chrome" 
      } 
     ], 
     "platforms": [ 
      { 
       "count": "1", 
       "id": "Macintosh" 
      } 
     ] 
    }, 
    "day": { 
     "shortUrlClicks": "0", 
     "longUrlClicks": "0" 
    }, 
    "twoHours": { 
     "shortUrlClicks": "0", 
     "longUrlClicks": "0" 
    } 
}, 
"result": { 
    "kind": "urlshortener#url", 
    "id": "http://goo.gl/2FIuvF", 
    "longUrl": "http://hike.com/?utm_source=facebook", 
    "status": "OK", 
    "created": "2015-09-22T13:45:53.645+00:00", 
    "analytics": { 
     "allTime": { 
      "shortUrlClicks": "1", 
      "longUrlClicks": "1", 
      "referrers": [ 
       { 
        "count": "1", 
        "id": "unknown" 
       } 
      ], 
      "countries": [ 
       { 
        "count": "1", 
        "id": "IN" 
       } 
      ], 
      "browsers": [ 
       { 
        "count": "1", 
        "id": "Chrome" 
       } 
      ], 
      "platforms": [ 
       { 
        "count": "1", 
        "id": "Macintosh" 
       } 
      ] 
     }, 
     "month": { 
      "shortUrlClicks": "1", 
      "longUrlClicks": "1", 
      "referrers": [ 
       { 
        "count": "1", 
        "id": "unknown" 
       } 
      ], 
      "countries": [ 
       { 
        "count": "1", 
        "id": "IN" 
       } 
      ], 
      "browsers": [ 
       { 
        "count": "1", 
        "id": "Chrome" 
       } 
      ], 
      "platforms": [ 
       { 
        "count": "1", 
        "id": "Macintosh" 
       } 
      ] 
     }, 
     "week": { 
      "shortUrlClicks": "1", 
      "longUrlClicks": "1", 
      "referrers": [ 
       { 
        "count": "1", 
        "id": "unknown" 
       } 
      ], 
      "countries": [ 
       { 
        "count": "1", 
        "id": "IN" 
       } 
      ], 
      "browsers": [ 
       { 
        "count": "1", 
        "id": "Chrome" 
       } 
      ], 
      "platforms": [ 
       { 
        "count": "1", 
        "id": "Macintosh" 
       } 
      ] 
     }, 
     "day": { 
      "shortUrlClicks": "0", 
      "longUrlClicks": "0" 
     }, 
     "twoHours": { 
      "shortUrlClicks": "0", 
      "longUrlClicks": "0" 
     } 
    } 
} 

}

在上面的JSON,我們怎麼能得到分析的存在 - >日 - >國家?

我想知道這些國家是否在一天之內存在,如果不存在,則顯示一些價值。如果它在那裏,它將嘗試獲取特定國家的計數。

我試圖從最後5小時沒有任何運氣。

if(arr.analytics.day.countries !== undefined) { 
     function thingscount(arr, platf) { 
      var x = arr.analytics.day.countries.map(function(el) { 
      return (platf.indexOf(el.id) != -1) ? parseInt(el.count) : 0; }); 
      var count = 0; 
      for (var i = 0; i < x.length; i++) count += x[i]; 
      return count; 
      }  

     var one = thingscount(arr, ["US"]); 

     }else{ 
      var one = 0; 

     } 

上面的代碼工作,如果有在日國家罰款,但有時,在我的JSON就沒有平臺的一部分,在這種情況下,它給了我

Uncaught TypeError: Cannot read property 'map' of undefined 

我需要一種方法檢查平臺是否存在,是否需要計數,如果沒有給變量賦予其他值。

UPDATE:

我用下面的代碼這獲得的計數。

當它具有IN鍵和值時,它會給出結果。但是當它沒有IN鍵時,它顯示'undefined count'錯誤。

var month_inclicks = arr.analytics.month.countries.filter(function(el) { return el.id == "IN"; })[0].count;

我們如何可以設置默認值,如果我們正在尋找的關鍵是不存在?

+0

這不是JSON。 http://jsonlint.com/ – Quentin

+0

對不起,有效的JSON更新。 – Anvy

回答

3

雖然這不是JSON,我假設它是一個javascript對象。這就是說,你會想利用hasOwnProperty方法或in關鍵字。

例子:

if (arr.total.limited.hasOwnProperty('platforms')) { //do stuff 

if ('platforms' in arr.total.limited) { //do something 
+0

剛糾正了json。你能再次檢查問題嗎? – Anvy

+0

仍然無效,請參閱http://jsonlint.com/。這就是說,它並沒有真正改變我的答案。如果你已經把它編組成一個JavaScript對象,你可以簡單地使用'hasOwnProperty'或'in'。 – CollinD

+0

我使用下面的代碼來計算IN的數量。 當它有IN鍵和值時,它給我的結果。但是當它沒有IN鍵時,它顯示'undefined count'錯誤。 'var month_inclicks = arr.analytics.month.countries.filter(function(el){return el.id ==「IN」;})[0] .count;' 如何設置默認值if我們正在尋找的關鍵不存在? – Anvy

2

我糾正了JSON。使用hasOwnProperty作爲@CollinD建議

var arr = { 
    total: { 
     limited: { 
      things: "451", 
      platforms: [{ 
       count: "358", 
       id: "Windows" 
      }, { 
       count: "44", 
       id: "X11" 
      }, { 
       count: "42", 
       id: "Macintosh" 
      }, { 
       count: "2", 
       id: "Linux" 
      }, { 
       count: "1", 
       id: "iPhone" 
      }, { 
       count: "1", 
       id: "iPod" 
      }] 

     } 
    } 
}; 

Object.prototype.hasOwnProperty()

console.log(arr.total.limited.hasOwnProperty('platforms')); 

DEMO

0

根據記錄,您可以通過降低滾動你的 'thingscount' 功能地圖和計數成一個操作:

var getCount = function getCount(ary, find) { 
    return ary.reduce(function (acc, record) { 
     if (find.indexOf(record.id) !== -1) acc += parseInt(record.count, 10); 
     return acc; 
    }, 0); 
}; 

用法在線:

if (arr.analytics.day.hasOwnProperty('countries')) { 
    var find = ['IN'], 
     count = arr.analytics.day.countries.reduce(function (acc, record) { 
      if (find.indexOf(record.id) !== -1) acc += parseInt(record.count, 10); 
      return acc; 
     }, 0); 
} 

或者與功能:

if (arr.analytics.day.hasOwnProperty('countries')) { 
    var count = getCount(arr.analytics.day.countries, ['US','IN']); 
} 
+0

用示例更新了我的代碼。 – Shilly

+0

如果使用空數組作爲輸入,則不會找到indexOf,並返回默認acc(umulator)值0。像'getCount(arr.analytics.day.countries,[]);'應該返回0. – Shilly

+0

if(find.indexOf(record.id)!== -1)'if'(find.indexOf (record.id)!== -1 && record.hasOwnProperty('count'))'如果你有一個國家有一個ID,但沒有計數,它不會拋出一個錯誤。 0來自於減少自己,因爲我們通過0作爲'acc'的起始值。所有處於'find'數組中並具有計數值的國家將被添加到我們傳入的0中。如果find數組爲空,或者數組中沒有國家有計數,則我們通過的0在從來沒有得到一個數字添加到它,所以結果保持爲0. – Shilly

相關問題