如何读取具有引号中具有标签/标识符的JSON数据?
我正在使用nodejs使用以下代码获取NPM注册表。
import fetch from "node-fetch";
let url = "https://registry.npmjs.com/[package-name]";
let settings = { method: "Get" };
fetch(url, settings)
.then(res => res.json())
.then((json) => {
console.log(json.versions)
});
因此,我在结果中得到的响应是:
{
_id: '123456789',
name: 'package-name',
'dist-tags': { latest: '2.0.0' },
versions: {
'2.0.0': '...'
}
...
}
我正在尝试阅读“版本”部分,但是我遇到的问题是,编号为2.0.0的版本是用引号包裹的,它可能会改变,但我想发现价值。我该如何阅读?
预先感谢,让我知道我错过的任何其他信息。
I'm using NodeJS to fetch the NPM registry with the code below.
import fetch from "node-fetch";
let url = "https://registry.npmjs.com/[package-name]";
let settings = { method: "Get" };
fetch(url, settings)
.then(res => res.json())
.then((json) => {
console.log(json.versions)
});
So, the response that I get in my result is:
{
_id: '123456789',
name: 'package-name',
'dist-tags': { latest: '2.0.0' },
versions: {
'2.0.0': '...'
}
...
}
I am trying to read the 'versions' section, but the issue I have is that the version number 2.0.0 is encased in quotes and it might change but I want to find that value. How can I read it?
Thanks in advance, and let me know any other info I missed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须检查版本是否具有所需的密钥。
或者,您可以通过
object.keys(wendment.versions)
迭代,并根据需要处理每个版本条目。You'll have to check if the versions has the key you want.
Or you could iterate through
Object.keys(response.versions)
and handle each version entry as you wish.