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序列化,不能 ...
随机推荐
- 手工sql注入判断是否存在注入点
1.加入单引号 ’提交,结果:如果出现错误提示,则该网站可能就存在注入漏洞.2.数字型判断是否有注入;语句:and 1=1 ;and 1=2 (经典).' and '1'=1(字符型)结果:分别返回不 ...
- Prefix.pch文件的用法
我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个 ...
- docker安装及概述
相关快捷键 退出:Ctrl-D or exit detach:Ctrl-P + Ctrl-Q Docker 核心技术 1.Namespace — 实现Container的进程.网络.消息.文件系统和主 ...
- 在双系统(Win7和Ubuntu Kylin)中卸载Ubuntu
由于以前学习Linux相关的知识,所以在win7的基础上装了ubuntu系统,最近在使用中老是出现一些问题,想将其卸载,于是在网上找了些相关方法. 我每次开机时,都会出现GRUB界面(我需要选择要进入 ...
- java获取本机ip的方法
直接上代码: public class LocalIPUtil { public static String getLocalIp(HttpServletRequest request){ Strin ...
- [题解](prufer)明明的烦恼
https://www.cnblogs.com/noip/archive/2013/03/10/2952520.html 以及高精(抄 #include<iostream> #includ ...
- GDPR(Cookie处理)
GDPR(Cookie处理) https://www.cnblogs.com/GuZhenYin/p/9154447.html 前言 时间一晃 ASP.NET Core已经迭代到2.1版本了. 迫不及 ...
- CoreCLR源码2
CoreCLR源码 前一篇我们看到了CoreCLR中对Object的定义,这一篇我们将会看CoreCLR中对new的定义和处理new对于.Net程序员们来说同样是耳熟能详的关键词,我们每天都会用到ne ...
- [RDL]中多行组列组占比报表制作
结果如下: 生意额占比表达式:=iif(Fields!生意额.Value is nothing,"",Fields!生意额.Value/sum(Fields!生意额.Value, ...
- Zeppelin的入门使用系列之使用Zeppelin来运行Spark SQL(四)
不多说,直接上干货! 前期博客 Zeppelin的入门使用系列之使用Zeppelin来创建临时表UserTable(三) 1. 运行年龄统计的Spark SQL (1) 输入Spark SQL时,必 ...