枚举任意对象键 - Javascript

时间:2017-10-09 03:52:30

标签: javascript json ecmascript-6 wikipedia-api

美好的一天,

我从维基百科解析这个结果时遇到了问题。

{
"batchcomplete": "",
"query": {
    "pages": {
        "252408": {
            "pageid": 252408,
            "ns": 0,
            "title": "Bulacan",
            "extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"
        }
    }
}

但它会创建一个随机数字键" 252408",我想解析"提取"的值。密钥而不声明随机数密钥和提取密钥。

5 个答案:

答案 0 :(得分:2)

您可以使用Object.values()来访问与随机密钥对应的值。



var res = {"batchcomplete": "","query":{"pages":{"252408": {"pageid": 252408,"ns": 0,"title": "Bulacan","extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"}}}};

let result = Object.values(res.query.pages)[0].extract;
console.log(result);




答案 1 :(得分:2)

您可以执行var keys = Object.keys(batchcomplete.query.pages);之类的操作来获取随机密钥(keys [0]),然后您可以使用该密钥获取提取属性。

答案 2 :(得分:2)

您还可以使用for-in循环来遍历对象



var wiki = {
"batchcomplete": "",
"query": {
    "pages": {
        "252408": {
            "pageid": 252408,
            "ns": 0,
            "title": "Bulacan",
            "extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"
        }
    }
}

}

   for(var key  in wiki["query"].pages){
    console.log(key);
   }

 




答案 3 :(得分:1)

因此,如果"pages"键下的对象始终只有一个键,并且您不知道它的值,则可以使用,

var randKeyObj = Object.keys(obj.query.pages)[0];

这将始终获取传入的对象的第一个或唯一的键。

答案 4 :(得分:0)

这是页面ID,而不是随机数。您可以使用formatversion=2 API参数来获取普通数组。