• JSON概述
     JSON(Java Script Object Notation)JS对象符号,通常JSON和XML是二选一的,JSON的数据格式很类似于JavaScript的对象
{
"pets": {
"name": "Jeffrey",
"species": "Giraffe"
}
}
  • .NET原生方式进行数据转换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
 
    public class JsonHelper
    {
        public static string ToJson<T>(T obj)
        {
            string result = String .Empty;
            try
            {
                System.Runtime.Serialization.Json. DataContractJsonSerializer serializer =
                new System.Runtime.Serialization.Json.DataContractJsonSerializer( typeof(T));
                using (System.IO.MemoryStream ms = new System.IO. MemoryStream())
                {
                    serializer.WriteObject(ms, obj);
                    result = System.Text. Encoding.UTF8.GetString(ms.ToArray());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
 
        public static string ToJsonFromByList<T>( List<T> vals)
        {
            System.Text. StringBuilder st = new System.Text.StringBuilder();
            try
            {
                System.Runtime.Serialization.Json. DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer (typeof(T));
 
                foreach (T city in vals)
                {
                    using (System.IO.MemoryStream ms = new System.IO. MemoryStream())
                    {
                        s.WriteObject(ms, city);
                        st.Append(System.Text. Encoding.UTF8.GetString(ms.ToArray()));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
 
            return st.ToString();
        }
 
        public static T ParseFormByJson<T>(string jsonStr)
        {
            T obj = Activator.CreateInstance<T>();
            using (System.IO.MemoryStream ms =
            new System.IO.MemoryStream (System.Text.Encoding.UTF8.GetBytes(jsonStr)))
            {
                System.Runtime.Serialization.Json. DataContractJsonSerializer serializer =
                new System.Runtime.Serialization.Json.DataContractJsonSerializer( typeof(T));
                return (T)serializer.ReadObject(ms);
            }
        }
    }  
  • Newtonsoft.json组件方式进行数据转换

采用Newtonsoft.json组件方式实现json数据的转换需要引用Newtonsoft.json组件。

    • 下载地址
    • 简易方式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using Newtonsoft.Json;
 
 
    public class JsonHelper
    {
        public static string ToJson<T>(T value)
        {
            return JsonConvert .SerializeObject(value,Formatting.None);
        }
 
        public static T FromJson<T>(string jsonText)
        {
            return JsonConvert .DeserializeObject<T>(jsonText); ;
        }
    }
    • 复杂可配置方式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using Newtonsoft.Json;
using System.IO;
 
   public static string ToJson<T>(T value)
        {
            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;
            StringWriter sw = new 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;
        }
      
        public static T FromJson<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;
            StringReader sr = new StringReader(jsonText);
            Newtonsoft.Json. JsonTextReader reader = new JsonTextReader(sr);
            T result = (T)json.Deserialize(reader, typeof(T));
            reader.Close();
            return result;
        }

JSON 数据转换的更多相关文章

  1. 【转】C#中将JSon数据转换成实体类,将实体类转换成Json

    http://wo13145219.iteye.com/blog/2022667 http://json2csharp.chahuo.com/ using System; using System.C ...

  2. Json数据与Json数据转换

    1.json数据 [{\"IS_DISTRIBUTOR_LIMIT\":0,\"PROVISION_PRICE\":null,\"PRO_STATUS ...

  3. 利用JAVA反射机制将JSON数据转换成JAVA对象

    net.sf.json.JSONObject为我们提供了toBean方法用来转换为JAVA对象, 功能更为强大,  这里借鉴采用JDK的反射机制, 作为简单的辅助工具使用,   有些数据类型需要进行转 ...

  4. VisualStudio2012轻松把JSON数据转换到POCO的代码

    原文:VisualStudio2012轻松把JSON数据转换到POCO的代码       在Visual Studio 2012中轻松把JSON数据转换到POCO的代码,首先你需要安装Web Esse ...

  5. VisualStudio2012轻松把JSON数据转换到POCO的代码(转)

    VisualStudio2012轻松把JSON数据转换到POCO的代码 在Visual Studio 2012中轻松把JSON数据转换到POCO的代码,首先你需要安装Web Essentials 20 ...

  6. JSON数据转换到POCO的代码

    转载:http://www.cnblogs.com/wintersun/archive/2012/09/14/2684708.html 在Visual Studio 2012中轻松把JSON数据转换到 ...

  7. json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 执行:JSONArray arr ...

  8. 递归系列——树型JSON数据转换问题

    JSON数据转换方式: 1.标准结构=>简单结构 var root = { id: 'root', children: [ { id: "1", children: [ { ...

  9. 将JSON数据转换成JAVA的实体类

    思路:首先将JSON格式的数据转换成JSONObject,然后将JSONObject转换成Java的实体类(其中类属性包括List等类型) Java实体类: SearchFilter 类 1 publ ...

随机推荐

  1. SQL笛卡尔积查询与关联查询性能对比

    首先声明一下,sql会用略懂,不是专家,以下内容均为工作经验,聊以抒情. 今天帮忙验证同事发布的端口时,查看了一下相关sql内容,发现其使用的sql语句会导致笛卡尔积现象,为了帮其讲解进行了如下分析: ...

  2. Mybatis Mapper文件中的一小坑

    前几天来一需求,实现过程中需要修改一个底层的查询接口,具体修改就是在where中添加一个条件,由于这个底层SQL使用的地方太多,所以就想着是用if加一标识符做个判断,传一个只有我会使用的参数,然后动态 ...

  3. Tomcat简单优化

    解决 有两种解决办法: 1)在Tomcat环境中解决 可以通过配置JRE使用非阻塞的Entropy Source. 在catalina.sh中加入这么一行:-Djava.security.egd=fi ...

  4. DAY10函数

    函数 函数就是可以重复利用的工具 函数可以完成指定代码块,函数就是是存放代码块的容器 函数的有点: 1.避免出现重复冗余的代码 2.让程序代码结构更清晰增加可读性 3 定义函数的语法 1. 函数名:使 ...

  5. lua语言自学知识点----Lua与.Net相互调用

    知识点: LuaInterface作用是用来完成Lua与C#的相互调用. LuaInterface核心库:1.luainterface.dll 用于C#读取lua(放在bin目录同级) 2.luane ...

  6. DCOS实践分享(6):基于DCOS的大数据应用分享

    Open DC/OS大中华区官方发布会在京隆重召开   DCOS领域诞生了一个100%开源的企业级Datacenter Operating System版本,即DC/OS.Linker Network ...

  7. PuppeteerSharp+AngleSharp的爬虫实战之汽车之家数据抓取

    参考了DotNetSpider示例, 感觉DotNetSpider太重了,它是一个比较完整的爬虫框架. 对比了以下各种无头浏览器,最终采用PuppeteerSharp+AngleSharp写一个爬虫示 ...

  8. 深港澳大湾区(深圳).NET技术交流会圆满成功

    2018年7月7日一场以.NET Core微服务和机器学习为主题的交流会成功在深圳职业技术学院落下帷幕.这次活动在短短的一周时间内,报名人数超过了170人,除了一些同学临时有事,基本都到现场了,特别感 ...

  9. Ooui.Wasm:浏览器中的.NET

    小型.NET Web框架Ooui作为Web程序集(WASM)在浏览器中完全运行 – 不需要服务器端的交互,涉及的所有文件都直接从浏览器执行,.NET Web框架Ooui可以作为其开发人员Frank A ...

  10. 交叉编译 tcpdump

    目录 1. 下载 tcpdump 2. 交叉编译 3. 相关说明 1. 下载 tcpdump 官网:http://www.tcpdump.org/ 2. 交叉编译 交叉编译libpcap: $ wge ...