美好的一天,

我从Wikipedia解析此结果时遇到问题。

{
"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”,我想解析“提取”键的值,而无需声明随机数键和提取键。

最佳答案

您可以使用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);

关于javascript - 枚举任意对象键-Javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46638606/

10-11 12:29