如何使用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']);
}