-2

I have an object as below:

Organization:
      { Id : 1, Name : name,  OrgType : { typeId : 1, name: tname }, employees : [{id:1, name:sm },{id:2, name:sm1 },{id:3, name:sm3 }]

so, here its complex object containing- Organization, OrgType and Employee list.

Problem, is I receive this collection from one service response and my application has different structure of object.

How can i deserialize above object to below structure:

Organization :
       Id changed to OrgId
       Name as Name,
       OrgType object changed to Type object
       Employees List object changed to EmpList object

In Type object - typeId changed to OtId, name changed to Name.

Employee object - id changed Id, name changed to EmployeeName

here, how to deserialize into above object where different property and also containing complex object.

user3711357
  • 1,425
  • 7
  • 32
  • 54
  • Deserialize to a model respresenting your json and then create the appropriate classes as you wish. – L.B Jul 10 '17 at 20:27
  • I dont' want to create class as it is already there , so not want to make two class for same entity. Any solution for transform or converter kind looking for. – user3711357 Jul 10 '17 at 20:35
  • OK than what is missing in your question: 1) Original *correct* json. 2) Your model you already have. 3) What you have tried so far – L.B Jul 10 '17 at 20:43
  • 1
    BTW: This is the answer you accepted. `You could deserialize into a dynamic type, and then map that to your destination model.` What is different than what I suggested.... Using temp classes are also compile time safe and can be thought a better approach – L.B Jul 10 '17 at 20:46
  • Not to add additional class into the solution. any converter, transformation or dynamic solution require. I can't add new class that's the problem. – user3711357 Jul 10 '17 at 20:53
  • sorry, did not mentioned about not to create class for entity. Thanks for the option provided. – user3711357 Jul 10 '17 at 21:05

1 Answers1

-1

Option 1.

You could deserialize into a dynamic type, and then map that to your destination model.

e.g. dynamic tempObject = JObject.Parse(serializedString);

Or option 2.

If you know the source model will remain constant, you could create a model that has the same structure as the incoming model and deserialize to that.

Then - for both options

You can use AutoMapper to map your source object to the destination object. There are lots of resources with details on how to accomplish this - here are is an example using a dynamic source object: https://github.com/AutoMapper/AutoMapper/wiki/Dynamic-and-ExpandoObject-Mapping

Steve Land
  • 4,852
  • 2
  • 17
  • 36