Newtonsoft.Json的使用
一、命名空间
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
二、方法
/// <summary>
/// 将对象转换为Json字符串
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ObjToJson(object value)
{
Type type = value.GetType();
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
json.NullValueHandling = NullValueHandling.Ignore;
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
System.IO.StringWriter sw = new System.IO.StringWriter();
Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
writer.Formatting = Formatting.None;
writer.QuoteChar = '"';
json.Serialize(writer, value);
string output = sw.ToString();
writer.Close();
sw.Close();
return output;
}
/// <summary>
/// 将Json字符串转换为对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonText"></param>
/// <returns></returns>
public static T JsonToObj<T>(string jsonText)
{
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
System.IO.StringReader sr = new System.IO.StringReader(jsonText);
Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);
T result = (T)json.Deserialize(reader, typeof(T));
reader.Close();
return result;
}
Newtonsoft.Json的使用相关推荐
- Newtonsoft.Json文件错误
今天,在一个项目中使用signalR,由于项目框架是.net 4.0,所以用signalR1.0版本,signalR使用需要newtonsoft.Json文件,它把原 newtonsoft.Json ...
- Newtonsoft.Json.dll 本地调用正常,服务器调用不正常
Newtonsoft.Json.dll 本地调用正常,服务器调用不正常的问题 请将System.Runtime.Serialization.dll 传到服务器项目bin文件夹中即可 转载于:https ...
- Newtonsoft.Json code
序列化 Product product = new Product(); product.ExpiryDate = new DateTime(2008, 12, 28);JsonSerializer ...
- 解决MVC Json序列化的循环引用问题/EF Json序列化循引用问题---Newtonsoft.Json
1..Net开源Json序列化工具Newtonsoft.Json中提供了解决序列化的循环引用问题: 方式1:指定Json序列化配置为 ReferenceLoopHandling.Ignore 方式2: ...
- Newtonsoft.Json 概述
有时候,在前后台数据交互或者APP与后台交互的时候,我们通常会使用Json进行数据交互,为此会使用到Newtonsoft.Json.dll 这个类库,这个类库非微软官方,但是下载量已经超过了数十万次, ...
- Newtonsoft.Json高级用法
手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)...
在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...
- Newtonsoft.Json的简单使用
2019独角兽企业重金招聘Python工程师标准>>> 什么是Newtonsoft.Json Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库. Ne ...
- [C#][Newtonsoft.Json] Newtonsoft.Json 序列化时的一些其它用法
Newtonsoft.Json 序列化时的一些其它用法 在进行序列化时我们一般会选择使用匿名类型 new { },或者添加一个新类(包含想输出的所有字段).但不可避免的会出现以下情形:如属性值隐藏(敏 ...
- Newtonsoft.Json(Json.net)的基本用法
Newtonsoft.Json(Json.net)的基本用法 添加引用: 使用NuGet,命令:install-package Newtonsoft.Json 实体类: public class Bo ...
最新文章
- Python学习--最完整的基础知识大全
- 新概念英语(1-61)A bad cold
- 请不要轻易使用 is_numberic 加入存在E字母
- CodeForces - 1323C Unusual Competitions(贪心)
- ls 显示目录下的内容和文件相关属性信息
- 不同文件类型输出及ContentType表
- 学习笔记6—pandas中ix,loc,iloc有什么区别?
- Git前世今生-版本控制软件的发展
- node项目打包如何隐藏html后缀,详解webpack打包nodejs项目(前端代码)
- /dev/hda5在linux中表示什么,linux
- 【hdoj1272】小希的迷宫(并查集+坑点)
- 封装el-select(全球国家名字及国家区号),select 输入框回显
- 低功耗微波雷达模块,LED庭院灯智能感应,雷达传感器助力节能减排
- 面试技巧-面试官的考题
- 华为云-软件产品案例分析
- Android Camera:从零开发一款相机APP Day01:前景
- 超像素分割(Superpixel Segmentation)发展
- 只说董超正在家里拴 3gp转换软件
- 【渝粤教育】电大中专电大中专职业生涯规划考试考核试题作业 题库
- C语言 函数调用时的数据传递