从 api 解释 JSON 的最佳方式是什么?
What's the best way to interpret JSON from an api ?
我正在开发一个使用维基百科 api 的应用程序,我正在努力寻找解释 ajax 请求给我的结果的最佳方式:
我得到的 JSON 看起来像这样:
{
"batchcomplete": "",
"query": {
"pages": {
"277029": {
"pageid": 277029,
"ns": 0,
"title": "Dog (zodiac)",
"index": 5,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Dog_2.svg/50px-Dog_2.svg.png",
"width": 50,
"height": 50
},
"pageimage": "Dog_2.svg",
"extract": "<p>The <b>Dog</b> (<b>狗</b>) is eleventh of the 12-year cycle of animals which appear in the Chinese zodiac related to the Chinese calendar. The <b>Year of the Dog</b> is associated with the Earthly Branch symbol <b>戌</b>. The character 狗 refers to the actual animal while 戌 refers to the zodiac animal..."
},
"552756": {
"pageid": 552756,
"ns": 0,
"title": "That Dog",
"index": 4,
"extract": "<p><b>That Dog</b> (styled as <b>that dog.</b>) is a Los Angeles-based rock band that formed in 1991 and dissolved in 1997, reuniting in 2011. The band originally consisted of Anna Waronker on lead vocals and guitar, Rachel Haden on bass guitar and vocals, her sister Petra Haden on violin and vocals..."
},
"779986": {
"pageid": 779986,
"ns": 0,
"title": "Dog Eat Dog",
"index": 7,
"extract": "<p><b>Dog Eat Dog</b> may refer to:</p>..."
},
"3279728": {
"pageid": 3279728,
"ns": 0,
"title": "Dog meat",
"index": 6,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Dog_Meat.jpg/50px-Dog_Meat.jpg",
"width": 50,
"height": 33
},
"pageimage": "Dog_Meat.jpg",
"extract": "<p><b>Dog meat</b> is the flesh and other edible parts derived from dogs. Historically, human consumption of dog meat has been recorded in many parts of the world. In the 21st century, most dog meat is consumed in China, Korea, and Vietnam, and it is still eaten or is legal to be eaten in other countries..."
}
}
}
}
我可以使用 result.query.pages 轻松访问这些页面,但是我需要独立访问每个页面,而不知道它们的 ID 是什么。我起初想,如果我必须访问第一页的标题,例如,使用
query.pages[Object.keys(pages)[0]].title
但我认为它可能过于复杂。有没有更有效的方法呢?
非常感谢!
既然我们在谈论 MediaWiki,请在您的请求中使用 formatversion=2,您将不会遇到现在的问题!
Changes to JSON output format
- Have action=query's "pages" be an array, instead of an object with page ids as keys that can be difficult to iterate.
您还可以检查此 post 以了解您当前的格式:
Extracting data from JSON
您可以使用以下方法遍历页面:
for (let id in query.pages) {
let page = query.pages[id];
// access title like this: page.title
}
要将页面转换为数组,请使用 Object.values(query.pages)
,如果您只想访问第一页的标题:
Object.values(query.pages)[0].title
if I had to access the title of the first page for exemple
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
var obj = { "batchcomplete": "", "query": { "pages": { "277029": { "pageid": 277029, "ns": 0, "title": "Dog (zodiac)", "index": 5, "thumbnail": { "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Dog_2.svg/50px-Dog_2.svg.png", "width": 50, "height": 50 }, "pageimage": "Dog_2.svg", "extract": "<p>The <b>Dog</b> (<b>狗</b>) is eleventh of the 12-year cycle of animals which appear in the Chinese zodiac related to the Chinese calendar. The <b>Year of the Dog</b> is associated with the Earthly Branch symbol <b>戌</b>. The character 狗 refers to the actual animal while 戌 refers to the zodiac animal..." }, "552756": { "pageid": 552756, "ns": 0, "title": "That Dog", "index": 4, "extract": "<p><b>That Dog</b> (styled as <b>that dog.</b>) is a Los Angeles-based rock band that formed in 1991 and dissolved in 1997, reuniting in 2011. The band originally consisted of Anna Waronker on lead vocals and guitar, Rachel Haden on bass guitar and vocals, her sister Petra Haden on violin and vocals..." }, "779986": { "pageid": 779986, "ns": 0, "title": "Dog Eat Dog", "index": 7, "extract": "<p><b>Dog Eat Dog</b> may refer to:</p>..." }, "3279728": { "pageid": 3279728, "ns": 0, "title": "Dog meat", "index": 6, "thumbnail": { "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Dog_Meat.jpg/50px-Dog_Meat.jpg", "width": 50, "height": 33 }, "pageimage": "Dog_Meat.jpg", "extract": "<p><b>Dog meat</b> is the flesh and other edible parts derived from dogs. Historically, human consumption of dog meat has been recorded in many parts of the world. In the 21st century, most dog meat is consumed in China, Korea, and Vietnam, and it is still eaten or is legal to be eaten in other countries..." } } }};
let [first] = Object.values(obj.query.pages);
console.log(first.title);
要访问这些值,您可以使用函数 forEach
和 Object.entries
来获取 pageId
和对象 page
var obj = { "batchcomplete": "", "query": { "pages": { "277029": { "pageid": 277029, "ns": 0, "title": "Dog (zodiac)", "index": 5, "thumbnail": { "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Dog_2.svg/50px-Dog_2.svg.png", "width": 50, "height": 50 }, "pageimage": "Dog_2.svg", "extract": "<p>The <b>Dog</b> (<b>狗</b>) is eleventh of the 12-year cycle of animals which appear in the Chinese zodiac related to the Chinese calendar. The <b>Year of the Dog</b> is associated with the Earthly Branch symbol <b>戌</b>. The character 狗 refers to the actual animal while 戌 refers to the zodiac animal..." }, "552756": { "pageid": 552756, "ns": 0, "title": "That Dog", "index": 4, "extract": "<p><b>That Dog</b> (styled as <b>that dog.</b>) is a Los Angeles-based rock band that formed in 1991 and dissolved in 1997, reuniting in 2011. The band originally consisted of Anna Waronker on lead vocals and guitar, Rachel Haden on bass guitar and vocals, her sister Petra Haden on violin and vocals..." }, "779986": { "pageid": 779986, "ns": 0, "title": "Dog Eat Dog", "index": 7, "extract": "<p><b>Dog Eat Dog</b> may refer to:</p>..." }, "3279728": { "pageid": 3279728, "ns": 0, "title": "Dog meat", "index": 6, "thumbnail": { "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Dog_Meat.jpg/50px-Dog_Meat.jpg", "width": 50, "height": 33 }, "pageimage": "Dog_Meat.jpg", "extract": "<p><b>Dog meat</b> is the flesh and other edible parts derived from dogs. Historically, human consumption of dog meat has been recorded in many parts of the world. In the 21st century, most dog meat is consumed in China, Korea, and Vietnam, and it is still eaten or is legal to be eaten in other countries..." } } }};
Object.entries(obj.query.pages).forEach(([pageId, page], index) => {
console.log(`'${page.title}' at index (${index})`);
});
将此格式转换为数组的最简单方法(如果您不能使用@GiannisMp 提到的 formatversion=2
技术)看起来是 Object.values(results.query.pages)
。一旦将它放入数组中,就可以使用标准数组技术对其进行操作,例如:
const results = {"batchcomplete": "", "query": {"pages": {"277029": {"extract": "<p>The <b>Dog</b> (<b>狗</b>) is eleventh of the 12-year cycle of animals which appear in the Chinese zodiac related to the Chinese calendar. The <b>Year of the Dog</b> is associated with the Earthly Branch symbol <b>戌</b>. The character 狗 refers to the actual animal while 戌 refers to the zodiac animal...", "index": 5, "ns": 0, "pageid": 277029, "pageimage": "Dog_2.svg", "thumbnail": {"height": 50, "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Dog_2.svg/50px-Dog_2.svg.png", "width": 50}, "title": "Dog (zodiac)"}, "3279728": {"extract": "<p><b>Dog meat</b> is the flesh and other edible parts derived from dogs. Historically, human consumption of dog meat has been recorded in many parts of the world. In the 21st century, most dog meat is consumed in China, Korea, and Vietnam, and it is still eaten or is legal to be eaten in other countries...", "index": 6, "ns": 0, "pageid": 3279728, "pageimage": "Dog_Meat.jpg", "thumbnail": {"height": 33, "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Dog_Meat.jpg/50px-Dog_Meat.jpg", "width": 50}, "title": "Dog meat"}, "552756": {"extract": "<p><b>That Dog</b> (styled as <b>that dog.</b>) is a Los Angeles-based rock band that formed in 1991 and dissolved in 1997, reuniting in 2011. The band originally consisted of Anna Waronker on lead vocals and guitar, Rachel Haden on bass guitar and vocals, her sister Petra Haden on violin and vocals...", "index": 4, "ns": 0, "pageid": 552756, "title": "That Dog"}, "779986": {"extract": "<p><b>Dog Eat Dog</b> may refer to:</p>...", "index": 7, "ns": 0, "pageid": 779986, "title": "Dog Eat Dog"}}}}
const pages = Object.values(results.query.pages)
const titles = pages.map(page => page.title)
console.log(titles)
我正在开发一个使用维基百科 api 的应用程序,我正在努力寻找解释 ajax 请求给我的结果的最佳方式:
我得到的 JSON 看起来像这样:
{
"batchcomplete": "",
"query": {
"pages": {
"277029": {
"pageid": 277029,
"ns": 0,
"title": "Dog (zodiac)",
"index": 5,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Dog_2.svg/50px-Dog_2.svg.png",
"width": 50,
"height": 50
},
"pageimage": "Dog_2.svg",
"extract": "<p>The <b>Dog</b> (<b>狗</b>) is eleventh of the 12-year cycle of animals which appear in the Chinese zodiac related to the Chinese calendar. The <b>Year of the Dog</b> is associated with the Earthly Branch symbol <b>戌</b>. The character 狗 refers to the actual animal while 戌 refers to the zodiac animal..."
},
"552756": {
"pageid": 552756,
"ns": 0,
"title": "That Dog",
"index": 4,
"extract": "<p><b>That Dog</b> (styled as <b>that dog.</b>) is a Los Angeles-based rock band that formed in 1991 and dissolved in 1997, reuniting in 2011. The band originally consisted of Anna Waronker on lead vocals and guitar, Rachel Haden on bass guitar and vocals, her sister Petra Haden on violin and vocals..."
},
"779986": {
"pageid": 779986,
"ns": 0,
"title": "Dog Eat Dog",
"index": 7,
"extract": "<p><b>Dog Eat Dog</b> may refer to:</p>..."
},
"3279728": {
"pageid": 3279728,
"ns": 0,
"title": "Dog meat",
"index": 6,
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Dog_Meat.jpg/50px-Dog_Meat.jpg",
"width": 50,
"height": 33
},
"pageimage": "Dog_Meat.jpg",
"extract": "<p><b>Dog meat</b> is the flesh and other edible parts derived from dogs. Historically, human consumption of dog meat has been recorded in many parts of the world. In the 21st century, most dog meat is consumed in China, Korea, and Vietnam, and it is still eaten or is legal to be eaten in other countries..."
}
}
}
}
我可以使用 result.query.pages 轻松访问这些页面,但是我需要独立访问每个页面,而不知道它们的 ID 是什么。我起初想,如果我必须访问第一页的标题,例如,使用
query.pages[Object.keys(pages)[0]].title
但我认为它可能过于复杂。有没有更有效的方法呢?
非常感谢!
既然我们在谈论 MediaWiki,请在您的请求中使用 formatversion=2,您将不会遇到现在的问题!
Changes to JSON output format
- Have action=query's "pages" be an array, instead of an object with page ids as keys that can be difficult to iterate.
您还可以检查此 post 以了解您当前的格式: Extracting data from JSON
您可以使用以下方法遍历页面:
for (let id in query.pages) {
let page = query.pages[id];
// access title like this: page.title
}
要将页面转换为数组,请使用 Object.values(query.pages)
,如果您只想访问第一页的标题:
Object.values(query.pages)[0].title
if I had to access the title of the first page for exemple
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
var obj = { "batchcomplete": "", "query": { "pages": { "277029": { "pageid": 277029, "ns": 0, "title": "Dog (zodiac)", "index": 5, "thumbnail": { "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Dog_2.svg/50px-Dog_2.svg.png", "width": 50, "height": 50 }, "pageimage": "Dog_2.svg", "extract": "<p>The <b>Dog</b> (<b>狗</b>) is eleventh of the 12-year cycle of animals which appear in the Chinese zodiac related to the Chinese calendar. The <b>Year of the Dog</b> is associated with the Earthly Branch symbol <b>戌</b>. The character 狗 refers to the actual animal while 戌 refers to the zodiac animal..." }, "552756": { "pageid": 552756, "ns": 0, "title": "That Dog", "index": 4, "extract": "<p><b>That Dog</b> (styled as <b>that dog.</b>) is a Los Angeles-based rock band that formed in 1991 and dissolved in 1997, reuniting in 2011. The band originally consisted of Anna Waronker on lead vocals and guitar, Rachel Haden on bass guitar and vocals, her sister Petra Haden on violin and vocals..." }, "779986": { "pageid": 779986, "ns": 0, "title": "Dog Eat Dog", "index": 7, "extract": "<p><b>Dog Eat Dog</b> may refer to:</p>..." }, "3279728": { "pageid": 3279728, "ns": 0, "title": "Dog meat", "index": 6, "thumbnail": { "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Dog_Meat.jpg/50px-Dog_Meat.jpg", "width": 50, "height": 33 }, "pageimage": "Dog_Meat.jpg", "extract": "<p><b>Dog meat</b> is the flesh and other edible parts derived from dogs. Historically, human consumption of dog meat has been recorded in many parts of the world. In the 21st century, most dog meat is consumed in China, Korea, and Vietnam, and it is still eaten or is legal to be eaten in other countries..." } } }};
let [first] = Object.values(obj.query.pages);
console.log(first.title);
要访问这些值,您可以使用函数 forEach
和 Object.entries
来获取 pageId
和对象 page
var obj = { "batchcomplete": "", "query": { "pages": { "277029": { "pageid": 277029, "ns": 0, "title": "Dog (zodiac)", "index": 5, "thumbnail": { "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Dog_2.svg/50px-Dog_2.svg.png", "width": 50, "height": 50 }, "pageimage": "Dog_2.svg", "extract": "<p>The <b>Dog</b> (<b>狗</b>) is eleventh of the 12-year cycle of animals which appear in the Chinese zodiac related to the Chinese calendar. The <b>Year of the Dog</b> is associated with the Earthly Branch symbol <b>戌</b>. The character 狗 refers to the actual animal while 戌 refers to the zodiac animal..." }, "552756": { "pageid": 552756, "ns": 0, "title": "That Dog", "index": 4, "extract": "<p><b>That Dog</b> (styled as <b>that dog.</b>) is a Los Angeles-based rock band that formed in 1991 and dissolved in 1997, reuniting in 2011. The band originally consisted of Anna Waronker on lead vocals and guitar, Rachel Haden on bass guitar and vocals, her sister Petra Haden on violin and vocals..." }, "779986": { "pageid": 779986, "ns": 0, "title": "Dog Eat Dog", "index": 7, "extract": "<p><b>Dog Eat Dog</b> may refer to:</p>..." }, "3279728": { "pageid": 3279728, "ns": 0, "title": "Dog meat", "index": 6, "thumbnail": { "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Dog_Meat.jpg/50px-Dog_Meat.jpg", "width": 50, "height": 33 }, "pageimage": "Dog_Meat.jpg", "extract": "<p><b>Dog meat</b> is the flesh and other edible parts derived from dogs. Historically, human consumption of dog meat has been recorded in many parts of the world. In the 21st century, most dog meat is consumed in China, Korea, and Vietnam, and it is still eaten or is legal to be eaten in other countries..." } } }};
Object.entries(obj.query.pages).forEach(([pageId, page], index) => {
console.log(`'${page.title}' at index (${index})`);
});
将此格式转换为数组的最简单方法(如果您不能使用@GiannisMp 提到的 formatversion=2
技术)看起来是 Object.values(results.query.pages)
。一旦将它放入数组中,就可以使用标准数组技术对其进行操作,例如:
const results = {"batchcomplete": "", "query": {"pages": {"277029": {"extract": "<p>The <b>Dog</b> (<b>狗</b>) is eleventh of the 12-year cycle of animals which appear in the Chinese zodiac related to the Chinese calendar. The <b>Year of the Dog</b> is associated with the Earthly Branch symbol <b>戌</b>. The character 狗 refers to the actual animal while 戌 refers to the zodiac animal...", "index": 5, "ns": 0, "pageid": 277029, "pageimage": "Dog_2.svg", "thumbnail": {"height": 50, "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Dog_2.svg/50px-Dog_2.svg.png", "width": 50}, "title": "Dog (zodiac)"}, "3279728": {"extract": "<p><b>Dog meat</b> is the flesh and other edible parts derived from dogs. Historically, human consumption of dog meat has been recorded in many parts of the world. In the 21st century, most dog meat is consumed in China, Korea, and Vietnam, and it is still eaten or is legal to be eaten in other countries...", "index": 6, "ns": 0, "pageid": 3279728, "pageimage": "Dog_Meat.jpg", "thumbnail": {"height": 33, "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Dog_Meat.jpg/50px-Dog_Meat.jpg", "width": 50}, "title": "Dog meat"}, "552756": {"extract": "<p><b>That Dog</b> (styled as <b>that dog.</b>) is a Los Angeles-based rock band that formed in 1991 and dissolved in 1997, reuniting in 2011. The band originally consisted of Anna Waronker on lead vocals and guitar, Rachel Haden on bass guitar and vocals, her sister Petra Haden on violin and vocals...", "index": 4, "ns": 0, "pageid": 552756, "title": "That Dog"}, "779986": {"extract": "<p><b>Dog Eat Dog</b> may refer to:</p>...", "index": 7, "ns": 0, "pageid": 779986, "title": "Dog Eat Dog"}}}}
const pages = Object.values(results.query.pages)
const titles = pages.map(page => page.title)
console.log(titles)