0

I have a pair of classes that look something like this:

public class Parent
{
    public int id { get; set; }
    public string name { get; set; }
    public List<Child> children { get; set; }
}
public class Child
{
    public int id { get; set; }
    public string name { get; set; }
}

In order to populate the parent class, I make an API call and deserialize the returned JSON which looks like this:

JSON

{
 “parent”:{
   “id”:”123”,
   “name”:”parent name”,
   “child”:{
       “id”:”456″
   },
 }
}

C#

var parent = new JavaScriptSerializer().Deserialize<List<Parent>>(jsonString);

I then use the id of the child to make another API call which returns more details about the child that I need to use to populate the parent:

{
 “child”:{
   “id”:”456”,
   “name”:”child name”
 }
}

How can I populate the rest of the Parent class with the data from the child JSON string?

10
  • It's not very clear whether you want to populate in C# or populate the embedded child object of the parent json? Commented Nov 29, 2017 at 21:02
  • Doesn't your second API call give you all the relevant information concerning child? Commented Nov 29, 2017 at 21:05
  • @danielcooperxyz What I would like is for the C# object (Parent class) to be populated with the combined data from both JSON strings Commented Nov 29, 2017 at 21:05
  • @KevinAvignon Yes, the second API call gives me all the relevant info, I just need to somehow associate it with the parent Commented Nov 29, 2017 at 21:06
  • 1
    I'm a bit lost here. In your example "partial" detail of child has only child id. So after first call, Parent.children list is populated but with partial info. Then you make another call and replace partial info with full, why not? Commented Nov 29, 2017 at 21:14

2 Answers 2

2

If I understood correctly, all you need is just make request for every partial child from Parent.children and then replace whole collection with full children information:

var parents = new JavaScriptSerializer().Deserialize<List<Parent>>(jsonString);
foreach (var parent in parents) {
    var fullChildren = new List<Child>();
    foreach (var partialChild in parent.children) {
        var fullChild = GetChildJsonById(partialChild.id);
        fullChildren.Add(fullChild);
    }
    // just replace whole stuff
    parent.children = fullChildren;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I haven't tested this, but I am fairly certain you can do something like:

var child = new JavaScriptSerializer().Deserialize<Child>(jsonString);

There shouldn't be more to it than that, assuming all you want is to update your child class.

This seems inefficient though, I don't know why the full child object isn't given from the first api call.

3 Comments

That works, but now I have two variables/objects with the data split. I would like var parent from my example above to contain all the info in the nested child As far as the full child object isn't given in the first call, that would be a question for the third party developer
Since you already have an instance of "parent", you can use "parent.children" along with @BrockReed code above to deserialize directly into the "children" property, right?
Then it seems the best you could do is use the Parent.children.id in your second call and that second call will populate the Child class properly.. that would give you the full 2 classes like you are looking for. Was that not everything you wanted?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.