【问题标题】:How to convert string to JSON returned by QnA Maker如何将字符串转换为 QnA Maker 返回的 JSON
【发布时间】:2018-12-14 18:05:19
【问题描述】:

我目前需要创建一个脚本,该脚本需要通过匹配我在 excel 文件中的问题来更新 QnA Maker 中 qna 对的元数据。我目前正在遵循 REST API 指南:

https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/quickstarts/csharp

我已经从 QnA Maker 检索了 qna 对,但由于它返回了所有 qna 信息的字符串值,我需要将其转换为 JSON 并将其与我从我的 excel 文件中检索到的 QnA 对象列表进行匹配。

 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: '无法反序列化 当前 JSON 对象(例如 {"name":"value"})转换为类型 'System.Collections.Generic.List`1[ExcelToQnAMaker.FAQs]' 因为 type 需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化。至 修复此错误或者将 JSON 更改为 JSON 数组(例如 [1,2,3]) 或更改反序列化类型,使其成为普通的 .NET 类型(例如 不是像整数这样的原始类型,不是像数组这样的集合类型 或列表)可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型中以强制它 从 JSON 对象反序列化。路径“qnaDocuments”,第 2 行,位置 17.'

我返回的结果字符串看起来像这样......

"{\r\n \"qnaDocuments\": [\r\n {\r\n \"id\": ....

这是我的常见问题解答课

 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; }
    }

【问题讨论】:

标签: c# json json.net qnamaker


【解决方案1】:

如果您查看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。

如果您想对最终模型进行一些映射,则必须将其作为额外的步骤。

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多