如何使用 node-fetch 访问 node.js 中 json 的对象
How to acess object of json in node.js using node-fetch
我的代码
const fetch = require('node-fetch');
fetch(`http://api.qrserver.com/v1/read-qr-code/?fileurl=https%3A%2F%2Fmedia.discordapp.net%2Fattachments%2F830357710473658368%2F872712396723081236%2Fqrcode.png`).then(res => res.json()).then(jsondata => console.log(jsondata))
Api 回应
[{"type":"qrcode","symbol":[{"seq":0,"data":"test1","error":null}]}]
我想获取 "data":"test1" 但如何获取?
你想从这个JSON
得到"test1"
:
[
{
"type":"qrcode",
"symbol":[
{
"seq":0,
"data":"test1",
"error":null
}
]
}
]
所以你可以这样做:
let firstResponseArrayElement = jsonData[0]; // {"type":"qrcode","symbol":[{"seq":0,"data":"test1","error":null}]}
let symbolValue = firstResponseArrayElement["symbol"]; // [{"seq":0,"data":"test1","error":null}]
let firstSymbol = symbolValue[0]; // {"seq":0,"data":"test1","error":null}
let symbolData = firstSymbol["data"]; // "test1"
为了更好地理解,我建议阅读 JSON 数据格式:https://www.json.org/json-en.html
我的代码
const fetch = require('node-fetch');
fetch(`http://api.qrserver.com/v1/read-qr-code/?fileurl=https%3A%2F%2Fmedia.discordapp.net%2Fattachments%2F830357710473658368%2F872712396723081236%2Fqrcode.png`).then(res => res.json()).then(jsondata => console.log(jsondata))
Api 回应
[{"type":"qrcode","symbol":[{"seq":0,"data":"test1","error":null}]}]
我想获取 "data":"test1" 但如何获取?
你想从这个JSON
得到"test1"
:
[
{
"type":"qrcode",
"symbol":[
{
"seq":0,
"data":"test1",
"error":null
}
]
}
]
所以你可以这样做:
let firstResponseArrayElement = jsonData[0]; // {"type":"qrcode","symbol":[{"seq":0,"data":"test1","error":null}]}
let symbolValue = firstResponseArrayElement["symbol"]; // [{"seq":0,"data":"test1","error":null}]
let firstSymbol = symbolValue[0]; // {"seq":0,"data":"test1","error":null}
let symbolData = firstSymbol["data"]; // "test1"
为了更好地理解,我建议阅读 JSON 数据格式:https://www.json.org/json-en.html