Home > Software design >  Merge two identical object model class after JsonConvert.DeserializeObject
Merge two identical object model class after JsonConvert.DeserializeObject

Time:06-24

C# and Newtonsoft.JSON

I've an object model class like this

class RolePerson
    {
        public NodeRolePerson[] Items { get; set; }
    }

class NodeRolePerson
{

    public bool active { get; set; }
    public string dateUva { get; set; }
    public bool delegated { get; set; }
    public NodeentityOrg entityOrg { get; set; }
    .....
    public string userUva { get; set; }
}
.........

Now i get data with

RolePerson myP1 = JsonConvert.DeserializeObject<RolePerson>(data1,settings);
RolePerson myP2 = JsonConvert.DeserializeObject<RolePerson>(data2,settings);

How can i have only one object with both myP1 and myP2 ?

I've tried with

List<RolePerson> trace;
trace.Add(myP1);
trace.Add(myP2);

but receive a compilation error 'local variable not assigned'.

Thanks so much.

CodePudding user response:

You never actually created a new list:

List<RolePerson> trace = new List<RolePerson>();
trace.Add(myP1);
trace.Add(myP2);

CodePudding user response:

try this

    var items = new List<NodeRolePerson>(myP1.Items);

    items.AddRange(myP2.Items);

    RolePerson trace = new RolePerson { Items = items.ToArray() };
  • Related