如何在自适应对话框 HttpRequest 中从 xml 转换为 json?
How to convert from xml to json within Adaptive dialog HttpRequest?
我是 Bot Framework 和 C# 的新手。我正在使用 Adaptive Dialog 和 Core Flight Booking Template (adaptive-dialog/03.core-bot) 构建一个聊天机器人。我想拨打 API 电话以获取天气信息。此 OpenWeather API 可以 return JSON、XML 或 HTML 格式的数据。当响应为 JSON 格式时,很容易访问键值对。但是当响应为 XML 格式时,所有内容都将转换为字符串并在响应中保存为 'content' ,我必须将其转换为 JSON 或字典,以便我可以访问详细信息。我需要在对话框中将 API 响应保存为 属性 以供以后参考。
我知道我们可以使用下面的代码将 XML 转换为 JSON,但问题是如何在自适应对话框中执行此操作。我试图在 HttpRequest 块中包含以下代码,但出现错误 “XmlDocument 是一种类型,在给定的上下文中无效。” 似乎您无法添加自己的自定义自适应对话框中的代码,您只能使用模板可以提供的内容,但现在 HttpRequest class 没有解析 XML 响应的选项。任何人都可以给我一些指导吗?谢谢!
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
在自适应对话框中生成 HttpRequest 的代码:
new IfCondition()
{
Condition = "conversation.Id != null",
Actions = new List<Dialog>()
{
new HttpRequest()
{
Url = "http://api.openweathermap.org/data/2.5/weatherq=Detroit&mode=xml&appid=appid={your api key}",
ResultProperty = "dialog.httpResponse",
Method = HttpRequest.HttpMethod.GET,
ResponseType = HttpRequest.ResponseTypes.Json
},
new Send Activity("${dialog.httpResponse}"),
new Send Activity("${dialog.httpResponse.content}")
}
}
下面显示了 HttpRequest 响应的样子。 OpenWeather API 响应(XML 格式)被转换为字符串作为 'content'.
的值
{
"statusCode": 200,
"reasonPhrase": "OK",
"headers":
{
"Server": "openresty",
"Date": "Tue, 14 Jul 2020 18:57:41 GMT",
"Connection": "keep-alive",
"X-Cache-Key": "/data/2.5/weather?mode=xml&q=detroit",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET, POST"
},
"content": "<?xml version="1.0" encoding="UTF-8"?>\n
<current>
<city id="4990729" name="Detroit">
<coord lon="-83.05" lat="42.33"></coord>
<country>US</country>
<timezone>-14400</timezone>
<sun rise="2020-07-14T10:08:16" set="2020-07-15T01:07:33"></sun>
</city>
<temperature value="301.11" min="300.15" max="302.59" unit="kelvin"></temperature>
<feels_like value="301.1" unit="kelvin"></feels_like>
<humidity value="44" unit="%"></humidity>
<pressure value="1019" unit="hPa"></pressure>
<wind>
<speed value="2.1" unit="m/s" name="Light breeze"></speed>
<gusts></gusts>
<direction></direction>
</wind>
<clouds value="75" name="broken clouds"></clouds>
<visibility value="16093"></visibility>
<precipitation mode="no"></precipitation>
<weather number="803" value="broken clouds" icon="04d"></weather>
<lastupdate value="2020-07-14T18:57:41"></lastupdate>
</current>"
}
您可以使用代码操作。
new IfCondition()
{
Condition = "conversation.Id != null",
Actions = new List<Dialog>()
{
new HttpRequest()
{
Url = "http://api.openweathermap.org/data/2.5/weatherq=Detroit&mode=xml&appid=appid={your api key}",
ResultProperty = "dialog.httpResponse",
Method = HttpRequest.HttpMethod.GET,
ResponseType = HttpRequest.ResponseTypes.Json
},
new CodeAction(async (dc, options) =>
{
var xml = dc.State.GetValue("dialog.httpResponse.content", () => "<root></root>");
var doc = new XmlDocument();
doc.LoadXml(xml);
var json = JsonConvert.SerializeXmlNode(doc);
dc.State.SetValue("dialog.json", json);
return await dc.EndDialogAsync();
}),
new SendActivity("${dialog.json}"),
}
}
我是 Bot Framework 和 C# 的新手。我正在使用 Adaptive Dialog 和 Core Flight Booking Template (adaptive-dialog/03.core-bot) 构建一个聊天机器人。我想拨打 API 电话以获取天气信息。此 OpenWeather API 可以 return JSON、XML 或 HTML 格式的数据。当响应为 JSON 格式时,很容易访问键值对。但是当响应为 XML 格式时,所有内容都将转换为字符串并在响应中保存为 'content' ,我必须将其转换为 JSON 或字典,以便我可以访问详细信息。我需要在对话框中将 API 响应保存为 属性 以供以后参考。
我知道我们可以使用下面的代码将 XML 转换为 JSON,但问题是如何在自适应对话框中执行此操作。我试图在 HttpRequest 块中包含以下代码,但出现错误 “XmlDocument 是一种类型,在给定的上下文中无效。” 似乎您无法添加自己的自定义自适应对话框中的代码,您只能使用模板可以提供的内容,但现在 HttpRequest class 没有解析 XML 响应的选项。任何人都可以给我一些指导吗?谢谢!
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
在自适应对话框中生成 HttpRequest 的代码:
new IfCondition()
{
Condition = "conversation.Id != null",
Actions = new List<Dialog>()
{
new HttpRequest()
{
Url = "http://api.openweathermap.org/data/2.5/weatherq=Detroit&mode=xml&appid=appid={your api key}",
ResultProperty = "dialog.httpResponse",
Method = HttpRequest.HttpMethod.GET,
ResponseType = HttpRequest.ResponseTypes.Json
},
new Send Activity("${dialog.httpResponse}"),
new Send Activity("${dialog.httpResponse.content}")
}
}
下面显示了 HttpRequest 响应的样子。 OpenWeather API 响应(XML 格式)被转换为字符串作为 'content'.
的值{
"statusCode": 200,
"reasonPhrase": "OK",
"headers":
{
"Server": "openresty",
"Date": "Tue, 14 Jul 2020 18:57:41 GMT",
"Connection": "keep-alive",
"X-Cache-Key": "/data/2.5/weather?mode=xml&q=detroit",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET, POST"
},
"content": "<?xml version="1.0" encoding="UTF-8"?>\n
<current>
<city id="4990729" name="Detroit">
<coord lon="-83.05" lat="42.33"></coord>
<country>US</country>
<timezone>-14400</timezone>
<sun rise="2020-07-14T10:08:16" set="2020-07-15T01:07:33"></sun>
</city>
<temperature value="301.11" min="300.15" max="302.59" unit="kelvin"></temperature>
<feels_like value="301.1" unit="kelvin"></feels_like>
<humidity value="44" unit="%"></humidity>
<pressure value="1019" unit="hPa"></pressure>
<wind>
<speed value="2.1" unit="m/s" name="Light breeze"></speed>
<gusts></gusts>
<direction></direction>
</wind>
<clouds value="75" name="broken clouds"></clouds>
<visibility value="16093"></visibility>
<precipitation mode="no"></precipitation>
<weather number="803" value="broken clouds" icon="04d"></weather>
<lastupdate value="2020-07-14T18:57:41"></lastupdate>
</current>"
}
您可以使用代码操作。
new IfCondition()
{
Condition = "conversation.Id != null",
Actions = new List<Dialog>()
{
new HttpRequest()
{
Url = "http://api.openweathermap.org/data/2.5/weatherq=Detroit&mode=xml&appid=appid={your api key}",
ResultProperty = "dialog.httpResponse",
Method = HttpRequest.HttpMethod.GET,
ResponseType = HttpRequest.ResponseTypes.Json
},
new CodeAction(async (dc, options) =>
{
var xml = dc.State.GetValue("dialog.httpResponse.content", () => "<root></root>");
var doc = new XmlDocument();
doc.LoadXml(xml);
var json = JsonConvert.SerializeXmlNode(doc);
dc.State.SetValue("dialog.json", json);
return await dc.EndDialogAsync();
}),
new SendActivity("${dialog.json}"),
}
}