首页 > 解决方案 > 如何递归获取类/树/json信息

问题描述

大家下午好,

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

我正在使用第三方 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 语句

任何帮助将不胜感激。

谢谢

标签: c#htmljsonrecursion

解决方案


您可以创建一种询问问题的方法,然后使用递归方式询问其他问题(如果存在)。写了一个如何递归调用该方法的示例(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);

推荐阅读