royco
royco

Reputation: 5529

How to create nested JSON objects and arrays with DataContractJsonSerializer?

I want to create JSON with nested arrays and objects like this:

{"orderId": "AF34235", 
 "recipients": [{"name": "Jane Doe", "address": "123 Main"},
                {"name": "Bob Doe", "address": "456 Broad"}],
 "sender": {"id": 123, "address": "789 Spruce"}
}

Is this possible with DataContractJsonSerializer? If so, what should my entity look like?

[DataContract]
class Order
{
    [DataMember(Name = "orderId")]
    public string OrderId { get; set; }

    // what next?

}

Upvotes: 0

Views: 2396

Answers (1)

I4V
I4V

Reputation: 35353

what should my entity look like?

See this site http://json2csharp.com/

public class Recipient
{
    public string name { get; set; }
    public string address { get; set; }
}

public class Sender
{
    public int id { get; set; }
    public string address { get; set; }
}

public class RootObject
{
    public string orderId { get; set; }
    public List<Recipient> recipients { get; set; }
    public Sender sender { get; set; }
}

Upvotes: 1

Related Questions