C# 中Newtonsoft.Json的安装和使用
官网参考:http://json.codeplex.com/
在程序包管理控制台,键入NuGet命令 install-package Newtonsoft.Json 安装Newtonsoft.Json
PM> install-package Newtonsoft.Json
正在安装“Newtonsoft.Json 6.0.5”。
已成功安装“Newtonsoft.Json 6.0.5”。
正在将“Newtonsoft.Json 6.0.5”添加到 MVCDemo.Model。
已成功将“Newtonsoft.Json 6.0.5”添加到 MVCDemo.Model。
原文:The quickest method of converting between JSON text and a .NET object is using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent and back again by mapping the .NET object property names to the JSON property names and copies the values for you.
译文:最快的JSON和文本之间的转换方法。NET对象是使用jsonserializer。该jsonserializer转换成JSON等效.NET对象 通过.NET对象属性名称映射成JSON属性名称,并且给你属性对应的Value值。(翻译水平太差,勿喷)
JsonConvert
原文:For simple scenarios where you want to convert to and from a JSON string the SerializeObject() and DeserializeObject() methods on JsonConvert provide an easy to use wrapper over JsonSerializer.
翻译:对于简单的情况下,包装在jsonserialize中的JsonConvert,他的serializeobject()和deserializeobject()方法,让对象和Json字符串之间的转换变的更加简单(翻译水平太差,勿喷)
用法:
Product product = new Product(); product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" }; 8string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//} Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
原文: SerializeObject and DeserializeObject both have overloads that take a JsonSerializerSettings object. JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods.
译文:SerializeObject和DeserializeObject 都有带有JsonSerializerSettings 对象的重载方法,JsonSerializerSettings 让你使用下面列出的许多jsonserializer设置时仍然使用简单的序列化方法。(翻译水平太差,勿喷)
//转换成Json字符串形式,返回格式:[{...},{...},{...}]
public static string ToJson(this object value, string defaultValue = "\"\"") {
if (value==null) {
return defaultValue;
}
else {
var json= JsonConvert.SerializeObject(value);//序列化
return json.Equals("null") ? defaultValue : json;
}
}
C# 中Newtonsoft.Json的安装和使用的更多相关文章
- FineUI中Newtonsoft.Json版本报错解决办法
1.清空bin下的Newtonsoft.Json.dll 2.使用Nuget安装最新版本的Newtonsoft.Json.dll,安装脚本为 Install-Package Newtonsoft.Js ...
- C#中Newtonsoft.Json 序列化和反序列化 时间格式
步骤 引用 using Newtonsoft.Json; using Newtonsoft.Json.Converters; 格式配置 IsoDateTimeConverter timeFormat ...
- 用NuGet安装NewtonSoft.json
因为要在C#里读取JSON字符串,资料查来查去,发现只能用第三方的NewtonSoft.json.本来.net也有自带的类库可以处理json,但TM的不停要你将JSON读进类对象里面.我靠,我只不过想 ...
- Newtonsoft.Json中的时间格式详解.
Newtonsoft.json是一款不错的序列化反序列化第三方组件,具体如何使用属于基础知识,此处不再讲解.看以下代码: public class OutgameEntity { public str ...
- 在.NET中使用Newtonsoft.Json转换,读取,写入的方法介绍
全局引用 using Newtonsoft.Json; using Newtonsoft.Json.Converters; //把Json字符串反序列化为对象 目标对象 = JavaScriptCon ...
- .NET中的Newtonsoft.Json.JsonConvert.SerializeObject(string a)
1.將string a 序列化為Json格式: 2.使用條件:將Newtonsoft.Json.dll作為引用添加到項目中.下载地址在这:http://json.codeplex.com/
- vs2008中使用Newtonsoft.Json
异常:找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()” 在使用Newtonsof ...
- SQL中采用Newtonsoft.Json处理json字符串
原文 SQL中采用Newtonsoft.Json处理json字符串 使用环境: SQL Server2005; VS2010; 关于SQL中部署CLR程序集的方法,网上一搜一大把,需要了解的自行查阅, ...
- asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析
下面我用一个实例来和大家分享一下我的经验,asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析. using Newtonsoft.Json; usin ...
随机推荐
- Mysql数据库基本配置
一 数据库基本配置包括编码方式 (安装环境是在linux下) 1.1 进入数据库 开启数据库服务:service mysqld start/restart(如果开启话可以重启) 关闭数据库服务:ser ...
- oracle11g 新特性 - rman自动备份控制文件延迟
OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 -6 ...
- Nginx服务器架构简析
一.Nginx的模块化 模块化结构的思想是一个很久的概念,但也正是成熟的思想造就了Nginx的巨大优越性. 我们知道Nginx从总体上来讲是有许多个模块构成的.习惯将Nginx分为5大模块分别为:核心 ...
- hdu 1427 速算24点
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1427 速算24点 Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A( ...
- 数组链表下标指针map list
1.时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间 ...
- Go defer延迟执行
defer用于延迟执行,可以类比于java或c++中的析构函数. 查看一段示例代码: func Contents(filename string) (string, error) { //打开文件 f ...
- NSAttributedString之设置字间距与行间距
// 调整行间距 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithSt ...
- 发布项目MVC4-EF6.0出错
出错: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFram ...
- 007--VS2013 C++ 显示位图半透明化
以后所有图片都放在根目录下: 如有另放,会特别注明 //全局变量HBITMAP bg,girl;HDC mdc; //起始坐标const int xstart = 50;const int ystar ...
- EF4.1之基础(实现Code First)
Code First:顾名思义:就是通过代码生成数据库----通过类生成数据库中对应的表: 首先定义两个类(就是建模的过程): public class Order { public int Orde ...