C
C#3mo ago
heyoka955

✅ Map json to a dto issue

I want to map my json to a dto this is my json!
{
"contact": {
"name": {
"first": "A",
"last": "K"
}
}
}
´´´

and this is my dto
{
"contact": {
"name": {
"first": "A",
"last": "K"
}
}
}
´´´

and this is my dto
public WixNameDto() { this.First = string.Empty; this.Last = string.Empty; } [JsonPropertyName("first")] public string First; [JsonPropertyName("last")] public string Last; } } ´´´ I used system.text.json
10 Replies
canton7
canton73mo ago
The first problem, as @0x686f676779303737 mentioned, is that those are fields, not properties How exactly are you calling System.Text.Json? Are you calling JsonSerializer.Deserialize<WixNameDto>(json)?
hoggy077
hoggy0773mo ago
-# summoned
heyoka955
heyoka955OP3mo ago
i declared it as properties but still dont work yes this way
hoggy077
hoggy0773mo ago
thats the other thing Canton wanted to point out
canton7
canton73mo ago
Right, so look at your JSON. In the outermost object, it's expecting a key "contact" (And then inside that it's expecting "name", and only inside that is it expecting "first" and "last") So you need to deserialize into an object which has a property "contact"
heyoka955
heyoka955OP3mo ago
okay i think it worked with properties thx really much! :kekw:
canton7
canton73mo ago
I don't see how it could possibly work without creating those extra objects, but ok! !close
Accord
Accord3mo ago
Closed!
heyoka955
heyoka955OP3mo ago
actually i had an object called contactdto and within there was namedto but it was too big to handle so i posted this
canton7
canton73mo ago
That's why I asked the question "Are you calling JsonSerializer.Deserialize<WixNameDto>(json)?", and you said "yes" >< Don't say "yes" if the answer is actually "no" in future, please
Want results from more Discord servers?
Add your server