❔ Assign JSON API Results To DTO

hey if I am returning json data from an API how do I assign the json data to my DTO? Meaning if I have a DTO that looks like this
public class TestDTO
{
public Data data { get; set; }
}
public class Data
{
public string fname { get; set; }
public string lname { get; set; }
public string phonenNumber { get; set; }
}
public class TestDTO
{
public Data data { get; set; }
}
public class Data
{
public string fname { get; set; }
public string lname { get; set; }
public string phonenNumber { get; set; }
}
but I only want to assign a value to fname and lname how would I set that up?
TestDTO td = null;

//code here

td = new TestDTO
{
//how do I get to fname here?
};
TestDTO td = null;

//code here

td = new TestDTO
{
//how do I get to fname here?
};
5 Replies
Angius
Angius2y ago
Either use a new DTO that has only what you need, or just
new TestDto {
Data = new DataDto {
//...
}
}
new TestDto {
Data = new DataDto {
//...
}
}
FoxTrotCharlieZulu
And wouldn't a new DTO still run into the same issue since the parent is always public Data data?
phaseshift
phaseshift2y ago
What 'same issue'?
FoxTrotCharlieZulu
sorry - no ZZZZZZZZZ is exactly correct. It works as expected
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.