简体   繁体   English

使用javascript访问JSON对象中的数据

[英]Accessing data within a JSON object using javascript

I have the following JSON object returned and I want to access all the "title" values. 我返回了以下JSON对象,我想访问所有的“标题”值。 I have been able to get a new object of all the "pages," but not the "titles." 我已经能够获得所有“页面”的新对象,但没有“标题”的对象。 I have tried everything except the correct thing! 除了正确的东西,我已经尝试了一切! The "similar" questions on here are straight forward array access questions. 这里的“相似”问题是直接的数组访问问题。 This seems to involve nested arrays. 这似乎涉及嵌套数组。 Please help! 请帮忙! Thanks. 谢谢。

{
    "batchcomplete": "",
    "continue": {
        "gsroffset": 15,
        "continue": "gsroffset||"
    },
    "query": {
        "pages": {
            "22989": {
                "pageid": 22989,
                "ns": 0,
                "title": "Paris",
                "index": 1
            },
            "59134": {
                "pageid": 59134,
                "ns": 0,
                "title": "Paris Commune",
                "index": 13
            },
            "61371": {
                "pageid": 61371,
                "ns": 0,
                "title": "Paris (disambiguation)",
                "index": 2
            },
            "64129": {
                "pageid": 64129,
                "ns": 0,
                "title": "Catacombs of Paris",
                "index": 11
            },
            "76286": {
                "pageid": 76286,
                "ns": 0,
                "title": "Disneyland Paris",
                "index": 15
            },
            "89106": {
                "pageid": 89106,
                "ns": 0,
                "title": "Paris–Brest–Paris",
                "index": 8
            },
            "357488": {
                "pageid": 357488,
                "ns": 0,
                "title": "Paris Saint-Germain F.C.",
                "index": 10
            },
            "868936": {
                "pageid": 868936,
                "ns": 0,
                "title": "Paris Gun",
                "index": 14
            },
            "2397134": {
                "pageid": 2397134,
                "ns": 0,
                "title": "Paris (Paris Hilton album)",
                "index": 6
            },
            "7618874": {
                "pageid": 7618874,
                "ns": 0,
                "title": "Paris syndrome",
                "index": 9
            },
            "11217925": {
                "pageid": 11217925,
                "ns": 0,
                "title": "Paris Hilton",
                "index": 7
            },
            "23528038": {
                "pageid": 23528038,
                "ns": 0,
                "title": "Paris Jackson (actress)",
                "index": 5
            },
            "30242372": {
                "pageid": 30242372,
                "ns": 0,
                "title": "Paris Agreement",
                "index": 4
            },
            "45259235": {
                "pageid": 45259235,
                "ns": 0,
                "title": "Paris Laxmi",
                "index": 12
            },
            "55340805": {
                "pageid": 55340805,
                "ns": 0,
                "title": "Paris Paris",
                "index": 3
            }
        }
    }
}

It wants me to add more details, but I don't know what else I can add. 它要我添加更多详细信息,但我不知道还能添加什么。

Try this: 尝试这个:

const data = //your json
const pages = data.query.pages
const titles = Object.keys(pages).map( key => pages[key].title )

Store the values from 'pages' in a variable, then use Object.keys to get as an array all keys within 'pages' and then using map operator you will iterate over those keys and for each one, return the attribute 'title' for this entry. 将“页面”中的值存储在变量中,然后使用Object.keys来获取“页面”中所有键的数组,然后使用map运算符遍历这些键,并为每个键返回属性“ title”此条目。

If you want to use lodash/underscore, something like this would work: 如果要使用lodash /下划线,则可以使用以下方法:

const dataPages = data["query"]["pages"];
let titles = [];
_.each(dataPages, (dataPage) => titles.push(dataPage["title"]))

This should get you a titles array! 这应该为您提供一个标题数组!

This uses Object.keys to enumerate over the pages in the JSON. 这使用Object.keys枚举JSON中的页面。

"use strict"
const DATA = //your Map
const pages = DATA.query.pages; 
const keys = Object.keys(pages); // returns an array of the ID numbers that are properties in each page
let titles = [keys.length]; // initialize an output array

for(let i=0; i < keys.length; i++){
    titles[i] = pages[keys[i]].title;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM