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序列化,不能 ...
随机推荐
- Unity开发Android应用优化指南(下)
http://forum.china.unity3d.com/thread-27044-1-1.html 在Unity开发Android应用优化指南(上)一文中,从游戏性能,脚本等方面进行了分析和总结 ...
- Pycharm 设置TextStyle
之前在脚本中选择了一个字符串, PyCharm会"高亮"所有相同的字符串, 但是我不满意这个"高亮"的颜色,因为和背景色太相似了,所以需要做一下操作,修改这个& ...
- [Xcode 实际操作]六、媒体与动画-(2)使用图形上下文转换图片为灰度图
目录:[Swift]Xcode实际操作 本文将演示如何将图片转换为灰度图. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class V ...
- 如何使用Xshell连接VMware上的Linux虚拟机
前序:最近开始研究Hadoop平台的搭建,故在本机上安装了VMware workstation pro,并创建了Linux虚拟机(centos系统),为了方便本机和虚拟机间的切换,准备使用Xshell ...
- day5字典作业详解
1.day5题目 1.有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2&q ...
- Java代码读取文件
用Java代码读取文件时,保持文件本来的格式(主要是保持换行),这点有时候比较重要.用代码实现也相当简单. private static void readFile() { StringBuilder ...
- phpcms9.6 注入分析
phpcms9.6 注入分析 漏洞促发点\phpcms\modules\content\down.php $a_k = trim($_GET['a_k']); if(!isset($a_k)) sho ...
- Codeforces 1168A(二分check)
关键是check.要注意到其实有了mid以后每个位置都是独立的,它能从哪走到哪是固定了的,只要从左到右尽量贪心压着最小值即可. #include <cstdio> const int ma ...
- python 遇到的一些问题和解决方法
安装crypto python3里面这个改成了pycryptodome 1. pip3 install pycryptodome 或者 pip3 install -i https://pypi.do ...
- 开启 PHP 错误提示配置步骤详解
PHP编码出错不提示,这对于开发来说,是很不方便的.下面讲解如何开启错误提示步骤: 1. 打开php.ini文件. 以我的ubuntu为例,这个文件在: /etc/php5/apache2 目录下. ...