1.序列化

            VehicleModelSearchingModel model = new VehicleModelSearchingModel() {
brandId = ,
modelIds="",
yearIds="",
startTime=DateTime.Parse("2016-04-01").ToString(),
endTime = DateTime.Now.ToString(),
plateColor = ,
hasPlate = ,
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; }
}

反序列化数组:

var idArr = JsonConvert.DeserializeObject<string[]>(idStr);

3.忽略某些属性

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

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

用法:

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

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

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

5.取出了中被标记为JsonProperty的字段,并获取对应的值:

VehicleModelSearchingModel model = new VehicleModelSearchingModel() {
brandId = ,
modelIds="",
yearIds="",
startTime = Convert.ToDateTime("2015/8/25 14:15:08"),
endTime = Convert.ToDateTime("2015/9/25 14:15:09"),
plateColor = ,
hasPlate = ,
plateNumber = "",
vehicleColor = "",
sourceIds = ""
}; var qry = typeof(VehicleModelSearchingModel).GetProperties().Where(p => p.GetCustomAttributes(typeof(JsonPropertyAttribute), true).Any());
object itemValue = new object(); foreach (var item in qry)
{
Console.WriteLine("{0}, {1}",
((JsonPropertyAttribute)(item.GetCustomAttributes(typeof(JsonPropertyAttribute), true)[])).PropertyName,
item.GetValue(model, null));
}

6.JContainer扩展类

public static class JContainerExtension
{
public static List<T> ToList<T>(this JContainer container)
{
var lst = container.ToList();
List<T> lstRet = new List<T>(); foreach (var item in lst)
lstRet.Add(JsonConvert.DeserializeObject<T>(item.ToString())); return lstRet;
}
}

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

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

Newtonsoft.Json初探的更多相关文章

  1. .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程

    JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...

  2. 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)

    在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...

  3. Newtonsoft.Json 自定义 解析协议

    在开发web api的时候 遇到一个要把string未赋值默认为null的情况改成默认为空字符串的需求 这种情况就需要自定义json序列话的 解析协议了 Newtonsoft.Json默认的解析协议是 ...

  4. Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6

    未能加载文件或程序集“Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6”或它的某一个 ...

  5. Newtonsoft.Json 序列化和反序列化 时间格式【转】

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  6. Newtonsoft.Json 版本冲突解决

    在做asp.net MVC 开发时,因为引用的dll 中使用了更高版本的 Newtonsoft.Json ,导致运行时发生错误, 查资料说是因为webApi使用了Newtonsoft.Json 导致了 ...

  7. JsonHelper developed by using Newtonsoft.Json.NET, Deserialize to <T> object , XmlToJson/JsonToXml, QuoteName by using JToken Path.

    namespace TestConsoleApplication { using System; using System.Diagnostics; using System.Threading; u ...

  8. WP8如何添加Newtonsoft.Json包

    WP8开发的时候如何使用Newtonsoft.Json包呢?我在网上包括官网下的DLL文件,添加引用时都给出了这样的提示: 而后在网上找到的解决办法是:使用NuGet程序包来添加. 首先点击工具--& ...

  9. [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类

    [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...

随机推荐

  1. aimOffset注意事项

    AimOffset的记录 AimOffset是什么,就是动画(相对于某个具体姿势比如待机动作的)叠加. AimOffset有什么用,简单说就是叠加动作,比如无双中骑马挥刀动作叠加. 注意步骤 1所有分 ...

  2. 洛谷P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...

  3. 2014-10-4 NOIP模拟赛

    1.某种密码(password.*) 关于某种密码有如下描述:某种密码的原文A是由N个数字组成,而密文B是一个长度为N的01数串,原文和密文的关联在于一个钥匙码KEY.若KEY=∑▒[Ai*Bi],则 ...

  4. vue、React Nactive的区别(转载)

    Vue与React的对比 Vue.js与React.js从某些反面来说很相似,通过两个框架的学习,有时候对一些用法会有一点思考,为加深学习的思索,特翻阅了两个文档,从以下各方面进行了对比,加深了对这两 ...

  5. jQuery EasyUI/TopJUI创建日期时间输入框

    jQuery EasyUI/TopJUI创建日期时间输入框 日期时间输入框组件 HTML 和日期输入框类似,日期时间输入框允许用户选择日期和指定的时间并按照指定的输出格式显示.相比日期输入框,它在下拉 ...

  6. RobotFramework学习笔记-Web自动化

    一.窗口关键字使用 1.当前浏览器弹出新的窗口 使用Select Window和Close Window处理弹出窗口.实际使用中Select Window不一定会一次选中,通常会结合Wait Unti ...

  7. JSPs only permit GET POST or HEAD的解决方案(REST风格)

    问题:原文链接 https://blog.csdn.net/tiberroot/article/details/76615727 看到很多人解决办法使用 @ResponseBody注解 这个意思是按照 ...

  8. Net Core2-JWT

    NET Core2 http://www.cnblogs.com/wyt007/category/1130278.html JWT 设计解析及定制 前言 上一节我们讲述的书如何使用jwt token, ...

  9. js获取ISO8601规范时间

    var d = new Date(); d.setHours(d.getHours(), d.getMinutes() - d.getTimezoneOffset()); console.log(d. ...

  10. 爱上MVC~Web.Config的Debug和Release版本介绍

    回到目录 对于web.config来说,我们不会陌生,主要对站点进行相关参数的配置,当它被修改后,IIS里对应的应用程序池会被重启,而对于config里的一些配置我们一般使用比较多的是数据连接串con ...