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



大家下午好,

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

我正在与第三方问答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语句

如有任何帮助,我们将不胜感激。

谢谢

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

最新更新