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. ssm重新开发计科院新闻网站

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  2. hdu1754(线段树单点替换&区间最值模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:中文题诶- 思路:线段树单点替换&区间最大值查询模板 代码: #include & ...

  3. cogs1612. 大话西游

    1612. 大话西游 http://www.cogs.pro/cogs/problem/problem.php?pid=1612 ★★   输入文件:westward.in   输出文件:westwa ...

  4. java 同时安装多版本问题(转)

    描述:刚到新公司,自己安装了jdk1.7和开发工具myeclipse10,但是由于公司项目的需要(具体原因不详细描述了),需要使用myeclipse6.5和jdk1.6.于是在切换jdk1.7和jdk ...

  5. luogu P2709 小B的询问 最简单的莫队

    块内按右端点sort,块外按左端点sort 话说我刚开始这么修改... inline )*(c[a[i]]-),--c[a[i]];} inline )*(c[a[i]]+),++c[a[i]];} ...

  6. 2019-CCPC广东省赛总结

    2018年11月第一次参加ICPC区域赛青岛赛区,打铁了! 2019年5月第一次参加CCPC广东省赛,4题滚粗,C题莫队TLE13发,只拿了个铜牌! 教训总结: 比赛时千万不能犹豫,不能犹豫,不能犹豫 ...

  7. Jmeter JDBC Request的使用

    1. JDBC Request 这个Sampler可以向数据库发送一个jdbc请求(sql语句),并获取返回的数据库数据进行操作.它经常需要和JDBC Connection Configuration ...

  8. 060 Permutation Sequence 排列序列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列.按大小顺序列出所有排列情况,并一一标记,可得到如下序列 (例如,  n = 3):   1."123"   2. & ...

  9. 51NOD 区间的价值 V2

    http://www.51nod.com/contest/problem.html#!problemId=1674 因为题目要求的只是& 和 | 这两个运算.而这两个运算产生的值是有限的. & ...

  10. IDEA对比文件

    和另一个文件对比:右击文件,选择另一个文件 和剪切板的内容对比:右击代码区域