如何使用Javascript读取JSON数组以获取值(如果存在)

如何使用Javascript读取JSON数组以获取值(如果存在),javascript,java,php,arrays,json,Javascript,Java,Php,Arrays,Json,这是我的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": { "shortUrlCl

这是我的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->countries的存在

首先,我想知道这些国家是否存在于白天,如果不存在,则显示出一些价值。如果它在那里,它将尝试获取特定国家的计数

我在过去的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;

        }
我需要一种方法来检查平台是否存在,是否需要计数,是否没有给变量赋予其他值

更新:


我正在使用下面的代码来获取中的计数

当它有键和值时,它会给我结果。但当它没有IN键时,它显示“undefined count”错误

var month\u inclicks=arr.analytics.month.countries.filter(函数(el){return el.id==“IN”;})[0]。计数


如果我们要查找的键不存在,我们如何设置默认值呢?

虽然这不是JSON,但我假设它是一个javascript对象。也就是说,您需要考虑使用
hasOwnProperty
方法或
in
关键字

例如:

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


虽然这不是JSON,但我假设它是一个javascript对象。也就是说,您需要考虑使用
hasOwnProperty
方法或
in
关键字

例如:

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


我已经更正了你的JSON。使用@CollinD建议的
hasOwnProperty

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"
            }]

        }
    }
};


我已经更正了你的JSON。使用@CollinD建议的
hasOwnProperty

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"
            }]

        }
    }
};


对于记录,您可以使用reduce:

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']);
}

为了便于记录,您可以使用reduce将“ThingScont”函数中的地图和计数滚动到一个操作中:

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']);
}

这不是JSON。抱歉,有效的JSON已更新。这不是JSON。抱歉,有效的JSON已更新。刚刚更正了JSON。你能再检查一下这个问题吗?仍然无效,请参阅。也就是说,这并没有真正改变我的答案。如果已经将其封送到javascript对象,则可以简单地利用
hasOwnProperty
in
。当它有键和值时,它会给我结果。但当它没有IN键时,它显示“undefined count”错误<代码>var month_inclicks=arr.analytics.month.countries.filter(函数(el){return el.id==“IN”;})[0]。计数如果我们要查找的密钥不存在,我们如何设置默认值?请在您的问题中发布这些类型的更新,而不是作为评论链。显然这种格式有点难读。只是更正了json。你能再检查一下这个问题吗?仍然无效,请参阅。也就是说,这并没有真正改变我的答案。如果已经将其封送到javascript对象,则可以简单地利用
hasOwnProperty
in
。当它有键和值时,它会给我结果。但当它没有IN键时,它显示“undefined count”错误<代码>var month_inclicks=arr.analytics.month.countries.filter(函数(el){return el.id==“IN”;})[0]。计数如果我们要查找的密钥不存在,我们如何设置默认值?请在您的问题中发布这些类型的更新,而不是作为评论链。这种格式显然有点难读。如果使用空数组作为输入,将找不到indexOf,并返回默认的acc(umulator)值0。比如
getCount(arr.analytics.day.countries,[])
应该返回0。将工件
if(find.indexOf(record.id)!=-1)
更改为
if(find.indexOf(record.id)!=-1&&record.hasOwnProperty('count'))
如果您的国家有id,但没有计数,它不会抛出错误。0来自于reduce本身,因为我们传入0作为'acc'的起始值。“find”数组中所有具有计数值的国家/地区都将添加到我们传入的0中。如果find数组为空,或者如果数组中没有任何国家/地区有计数,则传入的0永远不会向其中添加数字,因此结果保持为0。当我尝试使用该函数时,它给出了
Uncaught TypeError:Cannot read属性“reduce”of undefined
error。我放置了第一个函数,并按照第三个示例所示使用。当我使用第二个代码时,它给了我未定义的输出。然后你可能尝试在一个不存在的数组上使用它。如果要使用第二个代码,请使用检查是否具有属性“country”来更新它。或者只是用你原来的计算方法。祝你好运。第二个代码运行良好。但是我需要在页面中多次使用它,以便查找一个可以工作的函数:)该函数可能有什么问题?如果使用空数组作为输入,将找不到indexOf,并返回默认的acc(umulator)值0。比如
getCount(arr.analytics.day.countries,[])
应该返回0。将工件
if(find.indexOf(record.id)!=-1)
更改为
if(find.indexOf(record.id)!=-1&&record.hasOwnProperty('count'))
如果您的国家有id,但没有计数,它不会抛出错误。0来自于reduce本身,因为我们传入0作为'acc'的起始值。“find”数组中所有具有计数值的国家/地区都将添加到我们传入的0中。如果find数组为空,或者如果数组中没有任何国家/地区有计数,则传入的0永远不会向其中添加数字,因此结果保持为0。当我尝试使用该函数时,它给出了
Uncaught TypeError:Cannot read属性“reduce”of undefined
error。我放置了第一个函数,并按照第三个示例所示使用。当我使用第二公司时