我想在反序列化后得到嵌套的json对象结果,如下面的代码所示。
var matrixDto = new QuestionMatrixDto();
// First child Dto:
matrixDto.displayAnswers = "displayAnswers Test";
matrixDto.questionWeight = 10;
// Second child dto:
var essayDto = new QuestionEssayDto();
essayDto.addToBank= true;
essayDto.questionLength = 10;
// Parent Dto:
var obj = new V2ObjectDto();
obj.name = "object test";
obj.childObjects.Add(matrixDto);
obj.childObjects.Add(essayDto);
JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};
var serlizeObj = JsonConvert.SerializeObject(obj,settings );
var deserlizeObj = JsonConvert.DeserializeObject<V2ObjectDto>(serlizeObj,settings );
但是现在当我试图访问
时var matrixResult = deserlizeObj.childObjects.FirstOrDefault();
matrixResult.displayAnswers;
matrixResult.displayAnswers
不可访问且没有定义
我需要在反序列化后序列化对象之前的相同结果,但问题是我无法在反序列化后获得任何问题(或子Dto)属性值。
序列化结果:
{
"id": null,
"text": null,
"guid": null,
"containerId": null,
"name": "object test",
"testName": null,
"childObjects": [{
"questionText": null,
"testStatus": null,
"displayAnswers": "displayAnswers Test",
"questionWeight": 10,
"answers": [],
"questions": [],
"questionAnswersDto": [],
"userAnswer": {
"selectedAnswers": [],
"id": null,
"comments": null,
"questionId": null
},
"keywords": null,
"commentObj": {
"watermarkText": null,
"commentText": null,
"textType": null,
"userComment": null,
"isCorrect": false,
"totalCharacters": null
},
"questionSelectDto": {
"questionType": null,
"addToBank": false,
"numberOfAnswersAllow": null,
"displayAnsweras": null,
"id": null,
"text": null,
"guid": null,
"containerId": null,
"name": "",
"testName": null,
"childObjects": [],
"objectType": 0,
"sortOrder": 0,
"height": null,
"keywords": null
},
"feedBackText": null,
"questionType": null,
"addToBank": false,
"numberOfAnswersAllow": null,
"displayAnsweras": null,
"id": null,
"text": null,
"guid": null,
"containerId": null,
"name": "",
"testName": null,
"childObjects": [],
"objectType": 0,
"sortOrder": 0,
"height": null
}, {
"id": null,
"questionText": null,
"testStatus": null,
"questionWeight": 10,
"minCharecters": null,
"maxCharecters": null,
"commentObj": {
"watermarkText": null,
"commentText": null,
"textType": null,
"userComment": null,
"isCorrect": false,
"totalCharacters": null
},
"keywords": null,
"userAnswer": {
"essayText": null,
"id": null,
"comments": null,
"questionId": null
},
"feedBackText": null,
"questionType": null,
"addToBank": false,
"numberOfAnswersAllow": null,
"displayAnsweras": "displayAnswers for Test",
"text": null,
"guid": null,
"containerId": null,
"name": "",
"testName": null,
"childObjects": [],
"objectType": 0,
"sortOrder": 0,
"height": null
}
],
"objectType": 0,
"sortOrder": 0,
"height": null,
"keywords": null
}
Question Dto是ObjectDto like的子Dto
class QuestionDto : V2ObjectDto
注意父类属性将保持不变,但子类及其属性将随着问题类型而改变,正如我在上面的代码中提到的。
请帮助我解决这个问题,或者让我知道任何其他可能的解决方案。
在下面的例子中,这些类是一个层次结构。如果正确序列化,则正确执行反向操作。
public class BaseClass
{
public int ID { get; set; }
public ChildClass childObject { get; set; }
}
public class ChildClass
{
public int ID { get; set; }
public List<NodeClass> nodeObjects { get; set; }
}
public class NodeClass
{
public string Name { get; set; }
public int Order { get; set; }
}
现在使用
BaseClass bc = new BaseClass();
bc.ID = 1;
bc.childObject = new ChildClass()
{
ID = 1,
nodeObjects=new List<NodeClass>()
{
new NodeClass(){Name="Name 1",Order =1},
new NodeClass(){Name="Name 2",Order =2},
new NodeClass(){Name="Name 3",Order =3},
}
};
string json = JsonConvert.SerializeObject(bc);
BaseClass result = JsonConvert.DeserializeObject<BaseClass>(json);