【问题标题】:How to create nested JSON objects and arrays with DataContractJsonSerializer?如何使用 DataContractJsonSerializer 创建嵌套的 JSON 对象和数组?
【发布时间】:2013-04-15 21:58:29
【问题描述】:

我想用这样的嵌套数组和对象创建 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?

}

【问题讨论】:

    标签: .net json datacontractjsonserializer


    【解决方案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; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-27
      • 2019-02-16
      • 2023-02-17
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多