[英]Newtonsoft json - deserialize classes with same name, but different properties
I am consuming a REST API - which returns JSON.我正在使用 REST API - 它返回 JSON。 I deserialize the JSON using NewtonSOFT JSON in C#.
我在 C# 中使用 NewtonSOFT JSON 反序列化 JSON。
The returned JSON contanins an "Answer" object that contain another "Answer" object - problem is that the 2 "Answer" objects has different properties / definitions.返回的 JSON 包含一个“答案”object,其中包含另一个“答案”object - 问题是 2 个“答案”对象具有不同的定义/
How can that be handled in C# or in NewtonSoft?如何在 C# 或 NewtonSoft 中处理?
"answers": [
{
"tag": {
"id": 803,
"name": "Oplysninger om bestilling af tilstandsrapporten"
},
"option": false,
"answers": [
{
"label": "Vælg",
"value": "Ved hjælp af familie, venner eller bekendte mv",
"show_inline": false
}
],
"question": "Hvordan fandt du den bygningssagkyndige?",
"seller_question_id": 1
}
"answers" here is a property name, not a class name, so all you need to do is define your classes in a way that matches the pattern you've shown.这里的“answers”是一个属性名称,而不是 class 名称,因此您需要做的就是以与您显示的模式相匹配的方式定义您的类。 Having two different properties named
"answers"
doesn't present any particular complication.拥有两个名为
"answers"
的不同属性不会带来任何特殊的复杂性。
class Response
{
public IList<OuterAnswer> answers { get; set; }
}
class OuterAnswer
{
public Tag tag { get; set; }
public bool option { get; set; }
public IList<InnerAnswer> answers { get; set; }
public string question { get; set; }
public long seller_question_id { get; set; }
}
class InnerAnswer
{
public string label { get; set; }
public string value { get; set; }
public bool show_inline { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.