C#C
C#4y ago
Dasic

Newtonsoft.Json deserialize issue [Answered]

I have a json string. For example this one:
{"type":"accept","id":0,"ms_id":"00000","user_id":"000000000000","item_name":"Test Item","acc_name":"account"}

How can I deserialize some fields to a structure field in my class? For instance:
public class Order
{
    [JsonProperty("id")] public int Id { get; init; }
    [JsonProperty("item_name")] public string ItemName { get; init; }

    [JsonProperty("ms_id")] public long MessageId { get; init; }

    public Account Account { get; init; }
}

public struct Account
{
    [JsonProperty("user_id")] public long UserId { get; init; }
    [JsonProperty("acc_name")] public long Nickname { get; init; }
}

How can I deserialize the string to put these two fields in an Account field?
Was this page helpful?