码迷,mamicode.com
首页 > Web开发 > 详细

Newtonsoft.Json初探

时间:2016-04-14 17:51:08      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

1.序列化

            VehicleModelSearchingModel model = new VehicleModelSearchingModel() { 
                brandId = 0,
                modelIds="0",
                yearIds="0",
                startTime=DateTime.Parse("2016-04-01").ToString(),
                endTime = DateTime.Now.ToString(),
                plateColor = 0,
                hasPlate = 1,
                plateNumber = "",
                vehicleColor = "",
                sourceIds = ""
            };
            
            string s = JsonConvert.SerializeObject(model);
            Console.WriteLine(s);

其中,VehicleModelSearchingModel类:

[JsonObject(MemberSerialization.OptIn)]
    public class VehicleModelSearchingModel
    {
        [JsonProperty(propertyName: "resourceUuid")]
        public string sourceIds { get; set; }

        [JsonProperty(propertyName:"brandId")]
        public int? brandId { get; set; }
        public string brandId_str { get; set; }

        [JsonProperty(propertyName: "modelId")]
        public string modelIds { get; set; }
        public string modelIds_str { get; set; }

        [JsonProperty(propertyName: "vehicleYearId")]
        public string yearIds { get; set; }
        public string yearIds_str { get; set; }

        [JsonProperty(propertyName: "timeBegin")]
        public string startTime { get; set; }

        [JsonProperty(propertyName: "timeEnd")]
        public string endTime { get; set; }

        [JsonProperty(propertyName: "lpColor")]
        public int plateColor { get; set; }

        [JsonProperty(propertyName: "lp")]
        public string plateNumber { get; set; }

        [JsonProperty(propertyName: "haslp")]
        public int hasPlate { get; set; }

        [JsonProperty(propertyName: "vehicleColor")]
        public string vehicleColor { get; set; }
        public int confidence { get; set; }
        public bool isExactQuery { get; set; }
        public string orderby { get; set; }

    }

 

2.反序列化

string s = "{"code":204,"msg":"没有符合条件的数据!","resData":null}";
RetErr ret = JsonConvert.DeserializeObject<RetErr>(s);
Console.WriteLine("{0},{1}", ret.ABC, ret.msg);

其中RetErr类:

class RetErr
    {
         [JsonProperty(propertyName:"code")]
        public string ABC { get; set; }
        public string msg { get; set; }
        public string resData { get; set; }
    }

3.忽略某些属性

OptOut:默认值,类中所有公有成员会被序列化,如果不想被序列化,可以用特性JsonIgnore

OptIn:默认情况下,所有的成员不会被序列化,类中的成员只有标有特性JsonProperty的才会被序列化,当类的成员很多,但客户端仅仅需要一部分数据时,很有用

用法:

[JsonObject(MemberSerialization.OptIn)]
[JsonObject(MemberSerialization.OptOut)]

4.自定义序列化的字段名称

[JsonProperty(propertyName: "resourceUuid")]
public string sourceIds { get; set; }

暂时先了解了这些,因为项目中只用到了这些

参考:http://www.cnblogs.com/yanweidie/p/4605212.html

Newtonsoft.Json初探

标签:

原文地址:http://www.cnblogs.com/zhengwk/p/5391854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!