gpt4 book ai didi

javascript - 如何在nodejs应用程序中通过javascript使用azure Microsoft Translator Text的json输出?

转载 作者:行者123 更新时间:2023-11-30 06:13:44 25 4
gpt4 key购买 nike

我使用this code !用于通过 Microsoft Azure Translator Text 查询翻译。

这一行

console.log(JSON.stringify(body, null, 4));

在控制台中打印输入文本“bonjour”:

[
{
"detectedLanguage": {
"language": "fr",
"score": 1
},
"translations": [
{
"text": "Hello",
"to": "en"
}
]
}
]

我试图通过这种方式解析结果来获取字符串“Hello”:

console.log(body.translations.text)

我在控制台中得到了这个:

console.log(body.translations.text)
^
TypeError: Cannot read property 'text' of undefined

有什么想法吗?

最佳答案

您可以像这样获取文本的值:

console.log(body[0].translations[0].text);

enter image description here

body是一个数组,所以需要通过索引body[0]来获取数组中的值。

更新:我刚刚运行了该链接提供的整个代码,它也工作得很好。

request(options, function(err, res, body) {
console.log(JSON.stringify(body, null, 4));
console.log(body[0].translations[0].text);
});

关于javascript - 如何在nodejs应用程序中通过javascript使用azure Microsoft Translator Text的json输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com