2013-04-15 145 views
0

我想创建一个嵌套的数组和对象这样JSON:如何使用DataContractJsonSerializer创建嵌套的JSON对象和数组?

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

这可能与DataContractJsonSerializer?如果是这样,我的实体应该是什么样子?

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

    // what next? 

} 

回答

1

我应该我的实体是什么样子?

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; } 
}