【问题标题】:How to recursively grab class/tree/json information如何递归获取类/树/json信息
【发布时间】:2020-11-09 21:38:46
【问题描述】:

大家下午好,

我有一个我正在努力处理的问题

我正在使用第三方 Questions/Answers API,我问它一堆问题,它为他们提供可能的答案,这可能会导致更多问题

现在我只想将它们呈现在 HTML 中,但我的问题是,我正在将 JSON 从他们的 Web 服务反序列化为 C# 类,原始 JSON 看起来像

{
   "questionDetails":[
      {
         "templateId":"T1000515",
         "trees":[
            {
               "questions":[
                  {
                     "questionPhrase":"can this customer reported issue can be caused by accidental damage",
                     "answerType":"INT",
                     "questionId":"I100461"
                  },
                  {
                     "questionPhrase":"What damage is present on the display glass?",
                     "questionId":"Q100973",
                     "answers":[
                        {
                           "answerPhrase":"Single hairline crack with no point of impact",
                           "answerId":"A105795"
                        },
                        {
                           "answerPhrase":"Single hairline crack with a point of impact",
                           "answerId":"A105796"
                        },
                     ]
                  },
                  {
                     "questionPhrase":"Was the customer using any third party accessories on the display?",
                     "questionId":"Q217845",
                     "answers":[
                        {
                           "answerPhrase":"Yes",
                           "questions":[
                              {
                                 "questionPhrase":"Will the customer cover the cost of repair",
                                 "questionId":"I217846",
                                 "answers":[
                                 {
                                    "answerPhrase":"Yes",
                                    "answerId":"A20062"
                                 },
                                 {
                                     "answerPhrase":"No",
                                    "answerId":"A20063"
                                 }
                                 ]
                              }
                           ],
                           "answerId":"A20062"
                        },
                        {
                           "answerPhrase":"No",
                           "answerId":"A20063"
                        }
                     ]
                  }
               ],
               "treeId":"TREE101678"
            }
         ]
      }
   ]
}

标准问题->答案很好,但在一些示例中,它的问题->答案->问题->答案->问题->答案,我猜它可以递归到 10-15 个问题设备。

我不太确定如何解决这个问题,我首先可能会尝试创建一个通用树然后渲染该树,但我不确定是否需要使用反射来检查类,或者真的如何要说如果答案引起更多问题,请继续添加,不要写大量的 if 语句

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 我们似乎在同一天就开始解决这个问题。我一直在考虑将树渲染为一个平面 UL 列表,在分支上带有复选框和可能的文本或数字框,在描述路径的同一级别有一些隐藏类型输入,然后将其解析回 questionDetail。当然,它可能对一些 AJAX/jQuery 点击器更加用户友好,但它超出了我的能力范围......

标签: c# html json recursion


【解决方案1】:

您可以创建一种方法来询问问题,然后使用递归方式询问其他问题(如果存在)。写了一个如何递归调用方法的示例(AskQuestion),但只是一个简单的方法来展示如何使用递归方法。

// Starting point with a list of questions.
private static void AskQuestion(List<Questions> questions)
{
    foreach (Questions question in questions)
        Console.WriteLine(question.QuestionId + ": " + question.QuestionPhrase);
    
    Console.Write("Select Question: ");
    string ID = Console.ReadLine();

    Questions selectedQuestion = questions.Where(x => x.QuestionId.Equals(ID)).FirstOrDefault();
    if (selectedQuestion.Answers != null)
    {
        foreach (Answers answers in selectedQuestion.Answers)
            Console.WriteLine(answers.AnswerId + ": " + answers.AnswerPhrase);

        Console.Write("Select Answer: ");
        ID = Console.ReadLine();
        Answers answer = selectedQuestion.Answers.Where(x => x.AnswerId.Equals(ID)).FirstOrDefault();

        if (answer.Questions != null && answer.Questions.Count > 0)
            AskQuestion(answer.Questions); // <-- This is where you would recursively call the AskQuestion method for further questions.
        else
            Console.WriteLine("Hope you got the answer you were looking for");
    }
}

public class Questionaire
{
    [JsonProperty("questionDetails")]
    public List<QuestionDetails> QuestionDetails { get; set; }
}
public class QuestionDetails
{
    [JsonProperty("templateId")]
    public string TemplateId { get; set; }
    [JsonProperty("trees")]
    public List<Trees> Trees { get; set; }
}

public class Trees
{
    [JsonProperty("questions")]
    public List<Questions> Questions { get; set; }
    [JsonProperty("treeId")]
    public string TreeId { get; set; }
}

public class Questions
{
    [JsonProperty("questionPhrase")]
    public string QuestionPhrase { get; set; }
    [JsonProperty("answerType")]
    public string AnswerType { get; set; }
    [JsonProperty("questionId")]
    public string QuestionId { get; set; }
    [JsonProperty("answers")]
    public List<Answers> Answers { get; set; }
}
public class Answers
{
    [JsonProperty("answerPhrase")]
    public string AnswerPhrase { get; set; }
    [JsonProperty("answerId")]
    public string AnswerId { get; set; }
    [JsonProperty("questions")]
    public List<Questions> Questions { get; set; }
}

总的来说,你可以简单地做,

var obj = JsonConvert.DeserializeObject<Questionaire>(json);
AskQuestion(obj.QuestionDetails.First().Trees.First().Questions);

【讨论】:

  • 这应该非常适合我的尝试,谢谢!
  • 太棒了!嘿@DatenThielt,你知道当 answerType 为 INT 时如何返回值吗?只是通过给出 answerId 吗?或通过回答短语?其他的 answerType 是什么?我只会猜“收音机”和“复选框”...
  • 我很高兴我不是唯一一个与之抗争的人! (该死的苹果)但是,是的,他们真的没有让这个简单吗?当您意识到一些标记为必填问题实际上不是,而有些问题被标记为其他问题时,它会变得更加有趣!
  • 是的 - 大摇大摆是可以的,但有些部分只是让你自己弄清楚。顺便说一句,我刚刚检查了旧的 SOAP 文档,并且对 answerTypes 的描述要好得多!
  • 让我们松了一口气的是,INT 类型只是内联文本,尽管不是可选的,但不需要回复。 PS:提醒一下,新的 API 在 NDA 下,我们不会提及母舰......
猜你喜欢
  • 2012-07-14
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 2012-10-18
  • 2020-11-11
  • 2022-01-26
相关资源
最近更新 更多