如何将字符串转换为 QnA Maker 返回的 JSON
How to convert string to JSON returned by QnA Maker
我目前需要创建一个脚本,该脚本需要通过匹配 excel 文件中的问题来更新 QnA Maker 中 qna 对的元数据。我目前正在遵循 REST API 指南:
https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/quickstarts/csharp
我已经从 QnA Maker 中检索到 qna 对,但由于它返回了所有 qna 信息的字符串值,我需要将其转换为 JSON 并将其与我的 QnA 对象列表进行匹配从我的 excel 文件中检索。
public async static Task<string> GetQnAFromQnAMaker()
{
string getmethod = "/knowledgebases/{0}/{1}/qna/";
var method_with_id = String.Format(getmethod, kbid, env);
var uri = host + service + method_with_id;
Console.WriteLine("Calling " + uri + ".");
var response = await Get(uri);
return response;
}
我使用了 NewtonSoft 反序列化对象
List<FAQs> qnaMakerFaq = JsonConvert.DeserializeObject<List<FAQs>>(qnaFromQnAMaker.Result);
但是我得到这个错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the
current JSON object (e.g. {"name":"value"}) into type
'System.Collections.Generic.List`1[ExcelToQnAMaker.FAQs]' because the
type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To
fix this error either change the JSON to a JSON array (e.g. [1,2,3])
or change the deserialized type so that it is a normal .NET type (e.g.
not a primitive type like integer, not a collection type like an array
or List) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to
deserialize from a JSON object. Path 'qnaDocuments', line 2, position
17.'
我返回的结果字符串看起来像这样...
"{\r\n \"qnaDocuments\": [\r\n {\r\n \"id\": ....
这是我的常见问题解答Class
public class FAQs
{
public List<string> Questions { get; set; }
public string Answers { get; set; }
public string Classification { get; set; }
public string Division { get; set; }
public int Spid { get; set; }
public int Kbid { get; set; }
}
如果您查看 Download Knowledgebase 的 API 文档,您得到的响应应该类似于
{
"qnaDocuments": [
{
"id": 1,
"answer": "You can change the default message if you use the QnAMakerDialog. See this for details: https://docs.botframework.com/en-us/azure-bot-service/templates/qnamaker/#navtitle",
"source": "Custom Editorial",
"questions": [
"How can I change the default message from QnA Maker?"
],
"metadata": []
},
{
"id": 2,
"answer": "You can use our REST apis to manage your KB. See here for details: https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a073d9e041ad42d9baa",
"source": "Custom Editorial",
"questions": [
"How do I programmatically update my KB?"
],
"metadata": [
{
"name": "category",
"value": "api"
}
]
}
]
}
您尝试反序列化的模型应该匹配此结构...
public class KnowledgebaseResponse
{
public List<KnowledgebaseItem> QnaDocuments { get; set; }
}
public class KnowledgebaseItem
{
public int Id { get; set; }
public string Answer { get; set; }
public string Source { get; set; }
public List<string> Questions { get; set; }
public List<KeyValuePair<string, string>> MetaData { get; set; }
}
然后您可以正确反序列化 JSON。
如果您随后想对最终模型进行一些映射,则必须将其作为额外步骤进行。
我目前需要创建一个脚本,该脚本需要通过匹配 excel 文件中的问题来更新 QnA Maker 中 qna 对的元数据。我目前正在遵循 REST API 指南:
https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/quickstarts/csharp
我已经从 QnA Maker 中检索到 qna 对,但由于它返回了所有 qna 信息的字符串值,我需要将其转换为 JSON 并将其与我的 QnA 对象列表进行匹配从我的 excel 文件中检索。
public async static Task<string> GetQnAFromQnAMaker()
{
string getmethod = "/knowledgebases/{0}/{1}/qna/";
var method_with_id = String.Format(getmethod, kbid, env);
var uri = host + service + method_with_id;
Console.WriteLine("Calling " + uri + ".");
var response = await Get(uri);
return response;
}
我使用了 NewtonSoft 反序列化对象
List<FAQs> qnaMakerFaq = JsonConvert.DeserializeObject<List<FAQs>>(qnaFromQnAMaker.Result);
但是我得到这个错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ExcelToQnAMaker.FAQs]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'qnaDocuments', line 2, position 17.'
我返回的结果字符串看起来像这样...
"{\r\n \"qnaDocuments\": [\r\n {\r\n \"id\": ....
这是我的常见问题解答Class
public class FAQs
{
public List<string> Questions { get; set; }
public string Answers { get; set; }
public string Classification { get; set; }
public string Division { get; set; }
public int Spid { get; set; }
public int Kbid { get; set; }
}
如果您查看 Download Knowledgebase 的 API 文档,您得到的响应应该类似于
{
"qnaDocuments": [
{
"id": 1,
"answer": "You can change the default message if you use the QnAMakerDialog. See this for details: https://docs.botframework.com/en-us/azure-bot-service/templates/qnamaker/#navtitle",
"source": "Custom Editorial",
"questions": [
"How can I change the default message from QnA Maker?"
],
"metadata": []
},
{
"id": 2,
"answer": "You can use our REST apis to manage your KB. See here for details: https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a073d9e041ad42d9baa",
"source": "Custom Editorial",
"questions": [
"How do I programmatically update my KB?"
],
"metadata": [
{
"name": "category",
"value": "api"
}
]
}
]
}
您尝试反序列化的模型应该匹配此结构...
public class KnowledgebaseResponse
{
public List<KnowledgebaseItem> QnaDocuments { get; set; }
}
public class KnowledgebaseItem
{
public int Id { get; set; }
public string Answer { get; set; }
public string Source { get; set; }
public List<string> Questions { get; set; }
public List<KeyValuePair<string, string>> MetaData { get; set; }
}
然后您可以正确反序列化 JSON。
如果您随后想对最终模型进行一些映射,则必须将其作为额外步骤进行。