如何递归获取类/树/ json信息

时间:2020-07-20 15:24:46

标签: c# html json recursion

下午好,

我有一个要解决的问题

我正在使用第三方的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"
            }
         ]
      }
   ]
}

标准问题-> answer很好,但是有一些例子中的问题-> answer-> question-> answer-> question-> answer,我想对于某些问题,它最多可以递归10-15个问题设备。

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

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:0)

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