C# 解析json Newtonsoft.Json
Newtonsoft.Json.dll
public class ErrorInfo {
public error_response error_response { get; set; }
}
public class error_response
{
public string code { get; set; }
public string zh_desc { get; set; }
public string en_desc { get; set; }
}
//嵌套性数据
// {"error_response": {"code":"001","zh_desc":"信息无效"}} //解析方法一: 解析成一个对象实体
ErrorInfo obj = (ErrorInfo)JsonConvert.DeserializeObject(jsonText, typeof(ErrorInfo)); //解析方法二: 解析对象
JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText);
string code = jo["error_response"]["code"].ToString();
string zone = jo["error_response"]["zh_desc"].ToString();
/// <summary>
/// Json帮助类
/// </summary>
public class JsonHelper
{
private static void InitSetting()
{
Newtonsoft.Json.JsonSerializerSettings setting = new Newtonsoft.Json.JsonSerializerSettings();
JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
{ //日期类型默认格式化处理
setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//空值处理
setting.NullValueHandling = NullValueHandling.Ignore; return setting;
});
} /// <summary>
/// 将对象序列化为JSON格式
/// </summary>
/// <param name="o">对象</param>
/// <returns>json字符串</returns>
public static string SerializeObject(object o)
{
InitSetting();
string json = JsonConvert.SerializeObject(o);
return json.Replace("0001/1/1 0:00:00", "");//去除空日期 0001/1/1 0:00:00
}
public static object DeserializeObject(string o)
{
InitSetting();
return JsonConvert.DeserializeObject(o);
}
public static object DeserializeObject(string o, Type type)
{
InitSetting();
return JsonConvert.DeserializeObject(o, type);
} /// <summary>
/// 解析JSON字符串生成对象实体
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
/// <returns>对象实体</returns>
public static T DeserializeObject<T>(string json) where T : class
{
InitSetting();
object o = JsonConvert.DeserializeObject(json, typeof(T));
T t = o as T;
return t;
} }
JsonHelper
C# 解析json Newtonsoft.Json的更多相关文章
- 【转】C#解析Json Newtonsoft.Json
Newtonsoft.Json源码 Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用:DataC ...
- 屌丝技能--转Json(Newtonsoft.Json.dll)
妈妈再也不用为我转Json而担忧了!! 很简单,没什么好说明的,嗯! public class ShowTablePage<T> where T : class, new() { publ ...
- [C#][Newtonsoft.Json] Newtonsoft.Json 序列化时的一些其它用法
Newtonsoft.Json 序列化时的一些其它用法 在进行序列化时我们一般会选择使用匿名类型 new { },或者添加一个新类(包含想输出的所有字段).但不可避免的会出现以下情形:如属性值隐藏(敏 ...
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)
在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...
- C# 解析 json Newtonsoft果然强大,代码写的真好
C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程
JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...
- Newtonsoft.Json 把对象转换成json字符串
var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount ...
- Newtonsoft.Json 基本用法
Newtonsoft.Json 是.net 开源的一个json格式处理类库 官方网站:http://json.codeplex.com/ 在使用的项目引用Newtonsoft.Json库.平常使用的方 ...
- ASP.NET JSON的序列化和反序列化 之 Newtonsoft.Json
我们用到的类库为:Newtonsoft.Json,通过VS工具中NuGet程序包可以下载. 一:对象转json-序列化 public class Student { public int ID { g ...
随机推荐
- 在centos中使用vim编辑器
下面用编辑crontab举个例子: 在命令行输入 crontab -e 会直接进入vim编辑模式编辑crontab文件. 随后可以输入“i”进入insert模式 然后移动光标在指定的位置添加文字,可以 ...
- 转:Java反射教程
原文来自于:http://www.importnew.com/9078.html 什么是反射?反射有什么用处? 1. 什么是反射? “反射(Reflection)能够让运行于JVM中的程序检测和修改运 ...
- C# 在腾讯的发展(作者是微软连续10年的MVP)
本文首发我的微信公众号"dotnet跨平台", 内容得到大家热烈的欢迎,全文重新发布在博客,欢迎转载,请注明出处. .NET 主要的开发语言是 C# , .NET 平台泛指遵循EC ...
- HDOJ 1196 Lowest Bit(二进制相关的简单题)
Problem Description Given an positive integer A (1 <= A <= 100), output the lowest bit of A. F ...
- hdu5136:组合计数、dp
题目大意: 求直径长度为N的无根二叉树的个数(同构的只算一种) 分析: 分析发现直径长度不好处理!因此考虑把问题转化一下: 假设要求直径为N的二叉树 (1) 若N为偶数,将树从直径中点的边断开,则分成 ...
- IntelliJ IDEA常见问题及解决方法
1.IDEA导入项目后,源码.java文件图标带有红色圆圈,圆圈中间是个J: 该现象的原因是由于项目未把该路径指定为源码路径.解决方法: 在project Structure中(快捷键ctrl+alt ...
- 修改cmd的字体
通常打开的cmd默认的字体比较小,字体只有宋体和新宋体两种,如果要修改,需要通过修改注册表才行. 打开regedit后,找到如下路径HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...
- ORACLE_CLASS_ENDING
[JSU]LJDragon's Oracle course notes In the first semester, junior year Oracle考前复习 试题结构分析: 1.选择题2x10, ...
- Linux查找yum安装软件在系统中路径
find文件查找http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html
- HTML5 API's (Application Programming Interfaces)
New HTML5 API's (Application Programming Interfaces) The most interesting new API's are: HTML Geoloc ...