Newtonsoft.Json初探
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初探的更多相关文章
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程
JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)
在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...
- Newtonsoft.Json 自定义 解析协议
在开发web api的时候 遇到一个要把string未赋值默认为null的情况改成默认为空字符串的需求 这种情况就需要自定义json序列话的 解析协议了 Newtonsoft.Json默认的解析协议是 ...
- Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6
未能加载文件或程序集“Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6”或它的某一个 ...
- Newtonsoft.Json 序列化和反序列化 时间格式【转】
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- Newtonsoft.Json 版本冲突解决
在做asp.net MVC 开发时,因为引用的dll 中使用了更高版本的 Newtonsoft.Json ,导致运行时发生错误, 查资料说是因为webApi使用了Newtonsoft.Json 导致了 ...
- 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 ...
- WP8如何添加Newtonsoft.Json包
WP8开发的时候如何使用Newtonsoft.Json包呢?我在网上包括官网下的DLL文件,添加引用时都给出了这样的提示: 而后在网上找到的解决办法是:使用NuGet程序包来添加. 首先点击工具--& ...
- [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类
[.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...
随机推荐
- XXy
XXy codevs1003 帮我看看 #include<iostream> #include<cstdio> using namespace std; ],map[][],n ...
- 洛谷P3264 [JLOI2015]管道连接(斯坦纳树)
传送门 感觉对斯坦纳树还是有很多疑惑啊…… 等到时候noip没有爆零的话再回来填坑好了 //minamoto #include<iostream> #include<cstdio&g ...
- 剑指Offer的学习笔记(C#篇)-- 链表中倒数第K个点
题目描述 输入一个链表,输出该链表中倒数第k个结点. 一 . 数据结构基础概念普及(线性表). 线性表可分为顺序表与链表,它们是堆栈.队列.树.图等数据结构的实现基础. 顺序表,线性表的顺序存储结构是 ...
- Condition应用和源码分析
1.Condition实现一个队列public class BoundedQueue<T> { public List<T> q; //这个列表用来存队列的元素 private ...
- 通俗理解 React 高阶函数
定义:高阶组件就是一个函数,且该函数接受一个组件作为参数,并返回一个新的组件. A higher-order component is a function that takes a componen ...
- [题解]luogu_P2155_BZOJ_2186沙拉公主的困惑
题意求1~N!中与M!互质的数的个数, 首先证明gcd(a,b)=1时gcd(a-kb,b)=1 gcd(a,b)=1 gcd(a%b,b)=1 gcd(a-kb,b)=1 即a-kb与b互质 这样由 ...
- @Column 注解详情
@Column标记表示所持久化属性所映射表中的字段,该注释的属性定义如下: @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface ...
- Netty(1-2)Discard Client
一.DiscardClientHandler import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; import ...
- SmartWeatherAPI C#版
private string GetKey(string areaId, string type, string date, string appId, string privateKey) { va ...
- Bootcamp Win10蓝牙鼠标的问题
运行services.msc找到Bluetooth support service 把启动类型从手动改为自动 重新连接蓝牙鼠标