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的更多相关文章

  1. 【转】C#解析Json Newtonsoft.Json

    Newtonsoft.Json源码 Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用:DataC ...

  2. 屌丝技能--转Json(Newtonsoft.Json.dll)

    妈妈再也不用为我转Json而担忧了!! 很简单,没什么好说明的,嗯! public class ShowTablePage<T> where T : class, new() { publ ...

  3. [C#][Newtonsoft.Json] Newtonsoft.Json 序列化时的一些其它用法

    Newtonsoft.Json 序列化时的一些其它用法 在进行序列化时我们一般会选择使用匿名类型 new { },或者添加一个新类(包含想输出的所有字段).但不可避免的会出现以下情形:如属性值隐藏(敏 ...

  4. 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)

    在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...

  5. C# 解析 json Newtonsoft果然强大,代码写的真好

    C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...

  6. .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程

    JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...

  7. Newtonsoft.Json 把对象转换成json字符串

    var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount ...

  8. Newtonsoft.Json 基本用法

    Newtonsoft.Json 是.net 开源的一个json格式处理类库 官方网站:http://json.codeplex.com/ 在使用的项目引用Newtonsoft.Json库.平常使用的方 ...

  9. ASP.NET JSON的序列化和反序列化 之 Newtonsoft.Json

    我们用到的类库为:Newtonsoft.Json,通过VS工具中NuGet程序包可以下载. 一:对象转json-序列化 public class Student { public int ID { g ...

随机推荐

  1. How to install MP4box on CentOS 6

    How to install MP4box on CentOS 6 MP4Box is a MP4 multiplexer. It can import MPEG-4 video, DivX, Xvi ...

  2. jQueryMobile之listview

    效果如下: data-inset="true":为圆角 data-filter="true":带着过滤输入框 <!DOCTYPE html> < ...

  3. MySQL执行外部sql脚本

    1:-/mysql_test/test.sql create table student( sno int not null primary key auto_increment, sname ) n ...

  4. Angularjs Scope 原型链

    我们知道scope是可以继承的.scope的继承和js原型链是同一个概念. <div ng-controller="parentCtrl"> {{name}} < ...

  5. Getting Text Metrics in Firemonkey(delphiscience的博客)

    Firemonkey’s abstract TCanvas class has been providing the dimensions of the bounding rectangle of s ...

  6. 关于Action返回结果类型的事儿(下)

    原文:关于Action返回结果类型的事儿(下) using System;  using System.Collections.Generic;  using System.Linq;  using ...

  7. 2014-07-31 ASP.NET的母版页使用

    今天是在吾索实习的第17天.我在这天主要负责系统的骨架的搭建.首当其冲,要用的知识点就是ASP.NET母版页的使用了. ASP.NET的母版页有两种:一种是MasterPage,最常用也是最普通的母版 ...

  8. 批量将文件转换为UTF-8无BOM格式

    最近有一个项目需要迁移,要把文件全部转换成utf8格式的,本来想用python,后来听说PowerShell很是强大,就试着用了一下,果然好用啊! $list = Get-ChildItem .\ - ...

  9. mybatis dao无实现类的配置

    spring的配置文件 添加: <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">     ...

  10. CSS常用操作-分类