C
C#5w 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
canton75w 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
hoggy0775w ago
-# summoned
heyoka955
heyoka9555w ago
i declared it as properties but still dont work yes this way
hoggy077
hoggy0775w ago
thats the other thing Canton wanted to point out
canton7
canton75w 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
heyoka9555w ago
okay i think it worked with properties thx really much! :kekw:
canton7
canton75w ago
I don't see how it could possibly work without creating those extra objects, but ok! !close
Accord
Accord5w ago
Closed!
heyoka955
heyoka9555w 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
canton75w 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