我正在尝试将Angular应用程序中的POST发送到Azure Text Analytics API以进行关键字提取。我收到一条错误消息,告诉我我的邮件正文格式错误。我需要格式化消息正文以确保Azure可以通过这种方式理解它:
{
"documents": [
{
"language": "en",
"id": "1",
"text": "We love this trail and make the trip every year. The views are breathtaking and well worth the hike!"
},
{
"language": "en",
"id": "2",
"text": "Poorly marked trails! I thought we were goners. Worst hike ever."
},
{
"language": "en",
"id": "3",
"text": "Everyone in my family liked the trail but thought it was too challenging for the less athletic among us. Not necessarily recommended for small children."
},
{
"language": "en",
"id": "4",
"text": "It was foggy so we missed the spectacular views, but the trail was ok. Worth checking out if you are in the area."
},
{
"language": "en",
"id": "5",
"text": "This is my favorite trail. It has beautiful views and many places to stop and rest"
}
]
}
我有这个数据模型:
export class KeyFraze {
public id: number;
public language: string;
public text: string;
constructor(id: number, language: string, text: string) {
this.id = id;
this.language = language;
this.text = text;
}}
这是页面中的代码,在该页面中,我向azure发送文本(我在此处发送extractKeyFraze变量):
@Component({
selector: 'app-bot',
templateUrl: './bot.component.html',
styleUrls: ['./bot.component.css']
})
export class BotComponent implements OnInit {
allowSendMessage = false;
message = '';
messages: Message[] = [];
extractKeywordsFraze: KeyFraze[] = [];
constructor(private chatbotService: ChatbotService) {}
ngOnInit() {
}
onSend() {
if (this.message.length > 0) {
this.messages.push(new Message('user', this.message))
this.extractKeywordsFraze.push(new KeyFraze(1, 'en', this.message))
this.message = '';
this.chatbotService.extractKeywords(this.extractKeywordsFraze)
.subscribe(
(response) => console.log(response),
(error) => console.log(error)
);
}
}
有人可以告诉我如何格式化数据,使其以所需的格式显示吗?
答案 0 :(得分:1)
好,我知道了!
我设置了要发送的消息,如下所示:
this.body = {
"documents": [
{
"id": "1",
"language": "en",
"text": this.message
}
]
}
它现在可以工作。