0

I want to get nested json object result after its deserialization as in following code.

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 );

But now when I am trying to access

var matrixResult = deserlizeObj.childObjects.FirstOrDefault();
matrixResult.displayAnswers;

matrixResult.displayAnswers is not accessible and have no definition

I need same result as before serialize object after its deserialization, but the problem is that I am not able to get any question (or child Dto) attribute value after its deserialization.

Serialization result:

{
    "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 are the child dto of ObjectDto like

class QuestionDto : V2ObjectDto

Note the parent Class attribute will remain same but child class and its attributes will be changed with its question type as I mentioned in above code.

Please help me to sort out this issue or let me know about any other possible solution for it.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Is your question how to deserialize derived types with a common base type in the C# model? – ProgrammingLlama Mar 05 '21 at 08:38
  • Does this answer your question? [Json.net serialize/deserialize derived types?](https://stackoverflow.com/questions/8513042/json-net-serialize-deserialize-derived-types) – Drag and Drop Mar 05 '21 at 08:41
  • https://stackoverflow.com/questions/8241392/deserializing-heterogenous-json-array-into-covariant-list-using-json-net – Drag and Drop Mar 05 '21 at 08:43
  • its seems like its worked but i am getting another issue now .JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }; var serlizeObj = JsonConvert.SerializeObject(obj, settings); var deserlizeObj = JsonConvert.DeserializeObject (serlizeObj, settings); var matrixResult= deserlizeObj.childObjects.FirstOrDefault(); \ matrixResult.displayAnswers;//this line are not accesibe – Naveed Shahid Mar 05 '21 at 08:52
  • Why not follow a consistent pattern? When you follow a fixed pattern, Json's conversion and vice versa work well. – Meysam Asadi Mar 05 '21 at 09:25
  • @meysamasadi can you please give me an example regarding this? – Naveed Shahid Mar 05 '21 at 11:04
  • I will give you an example that I use this method myself – Meysam Asadi Mar 05 '21 at 13:02

1 Answers1

0

In the following example, the classes are in a hierarchical structure. If it is serialized correctly, the reverse operation will be performed correctly.

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; }
}

now use

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);
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17