如何使用Javascript读取JSON数组以获取存在的值
How to read the JSON Array with Javascript to get the value if it exists
这是我的JSON代码。我将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中,我们如何得到analytics -> day ->国家的存在?
我首先想知道这些国家是否以天为单位存在,如果不是,显示一些价值。如果它存在,它将尝试获取特定国家的计数。
我试了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
我需要一种方法来检查平台是否存在,是否需要计数,是否没有给变量赋其他值
更新:我使用下面的代码来获取IN的计数。
当它有IN键和值,它给我的结果。但是当它没有IN键时,它会显示'undefined count'错误。
var month_inclicks = arr.analytics.month.countries.filter(function(el) { return el.id == "IN"; })[0].count;
如果要查找的键不存在,如何设置默认值?
虽然这不是JSON,但我假设它是javascript对象。话虽如此,您需要考虑使用hasOwnProperty
方法或in
关键字。
的例子:
if (arr.total.limited.hasOwnProperty('platforms')) { //do stuff
或
if ('platforms' in arr.total.limited) { //do something
我已经更正了你的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'));
演示作为记录,您可以使用reduce:
将'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']);
}
相关文章:
- 如果 document.title 不存在,如何获取页面标题名称
- 如何获取 JSON 数组下存在的相应值
- 如何获取数据属性中存在的现有JSON并更新它
- 画布:当存在*border*时,获取鼠标坐标
- 推进器的存在可以't使用angularjs和golang获取成员
- 在Chrome上获取z索引值时存在差异
- 是否仍然存在获取Rest服务调用以返回纯字符串的方法
- 是否有任何跨浏览器方法可以获取带有滚动条的 DIV 宽度(如果存在)
- jQuery 获取搜索选择器,如果元素不存在
- 如何获取使用java脚本的网页中存在的所有链接
- var 存在或 var 获取值语法
- Javascript 正则表达式,用于在问号之前获取字符串(如果存在)
- 检查通过服务器内联加载的现有JSON obj,以确定工厂是否应该调用以获取相同的obj'还不存在
- 如何获取ajax请求响应中存在的图像的位置,并使用位置绘制线
- 是否可以在DOM中存在元素之前获取值
- 向javascript关联数组添加新值“;在飞行中”;当试图通过不存在的键获取值时
- 获取JavaScript对象中存在的所有函数(包括嵌套函数)
- 正在获取请求't通过访问控制检查:否'访问控制允许来源'标头存在于请求的资源上
- 如何使用Javascript读取JSON数组以获取存在的值
- 比较对象并只获取存在于所有对象中的对象