• 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. docker exec 系统找不到指定的路径。

    相关问题和答案 >docker exec -it a1 echo "hello..." > /var/www/html/index.html 系统找不到指定的路径. & ...

  2. react-native 打包成apk 文件

    用android studio 打包成apk 文件 js build 执行: react-native bundle --platform android --dev false --entry-fi ...

  3. ASP.NET Core 2.1对GDPR的支持

    欧盟的<通用数据保护条例>(General Data Protection Regulation,以下简称 GDPR)已经于 2018 年 5 月 25 日正式施行.GDPR 涵盖了包括数 ...

  4. Java线程状态Jstack线程状态BLOCKED/TIMED_WAITING/WAITING解释

    一.线程5种状态 新建状态(New) 新创建了一个线程对象. 就绪状态(Runnable) 线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行,等待获 ...

  5. java web项目get,post请求参数中文乱码解决

    [转载]原文地址:https://www.cnblogs.com/tom-plus/p/6392279.html 在开发过程中,有时候会碰到get,post请求参数中文乱码. 原因: Http请求传输 ...

  6. [Swift]LeetCode36. 有效的数独 | Valid Sudoku

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  7. Python面向对象中的类和对象

    类和对象 目标 类和对象的概念 类和对象的关系 类的设计 01. 类和对象的概念 类 和 对象 是 面向对象编程的 两个 核心概念 1.1 类 类 是对一群具有 相同 特征 或者 行为 的事物的一个统 ...

  8. 剑指Spring源码(一)

    Spring,相信每个Java开发都用过,而且是每天都在用,那强大又神秘的IoC,AOP,让我们的开发变得越来越简单,只需要一个注解搞定一切,但是它内部到底是什么样子的呢?跟着我,一起探究Spring ...

  9. RSA算法原理——(2)RSA简介及基础数论知识

    上期为大家介绍了目前常见加密算法,相信阅读过的同学们对目前的加密算法也算是有了一个大概的了解.如果你对这些解密算法概念及特点还不是很清晰的话,昌昌非常推荐大家可以看看HTTPS的加密通信原理,因为HT ...

  10. 4.Django模板语言和分页

    继承 extends 子版只能继承一个父模板 1.父模板 master.html <!DOCTYPE html> <html lang="en"> < ...