如何在 nodejs 应用程序中使用 json azure Microsoft Translator Text 的 javascript 输出?
How to use json output of azure Microsoft Translator Text with javascript in nodejs app?
我用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);
body是一个数组,所以需要通过索引获取数组中的值,body[0]。
更新:
我只是 运行 那个 link 提供的整个代码,它也能正常工作。
request(options, function(err, res, body) {
console.log(JSON.stringify(body, null, 4));
console.log(body[0].translations[0].text);
});
我用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);
body是一个数组,所以需要通过索引获取数组中的值,body[0]。
更新: 我只是 运行 那个 link 提供的整个代码,它也能正常工作。
request(options, function(err, res, body) {
console.log(JSON.stringify(body, null, 4));
console.log(body[0].translations[0].text);
});