• 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. 1.Git安装

    1.安装 首先下载安装包https://git-scm.com/downloads/ 双击安装任意盘符,双击之后一路Next,当然也可以修改默认配置 安装结束!

  2. IntelliJ IDEA配置Tomcat和Lombok

    Tomcat的安装和配置 Tomcat 是在SUN公司的JSWDK(JavaServer Web DevelopmentKit)的基础上发展而来的一个优秀的Servlet容器,其本身完全是由Java编 ...

  3. Oracle数据库在plsql中文乱码,显示问号????

    1.错误显示结果: 2.错误的原因: SqlPlus乱码与操作系统用户中的NLS_LANG这个环境变量有关系,如果这个与数据库字符集不一致的话就会产生乱码 3.解决方法1:(配置环境变量) 1.sel ...

  4. BUAA_OO Summary——多项式求导问题

    从C.DS.计组一路折磨过来, 几乎都在采用过程化.函数式的编程思想.初接触面向对象的项目开发,经过了三周的对多项式求导问题的迭代开发,经历了设计.coding.测评环节,算是对面向对象有了一定的认识 ...

  5. post数据时报错:远程服务器返回错误: (400) 错误的请求。

    网上查了多种方法,有不少说法,报400说是传的数据格式不对,最后的结论确实是数据格式不对. Content_Type为:application/json,配的数据格式有些麻烦,特别数多层,单层还好.例 ...

  6. Win10系统下在国内访问Tensorflow官网

    1.修改hosts文件 目录:     C:\Windows\System32\drivers\etc 添加: #TensorFlow start64.233.188.121 www.tensorfl ...

  7. android颜色color.xml设置

     XML Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  8. ASP.NET Core Web App应用第三方Bootstrap模板

    引言 作为后端开发来说,前端表示玩不转,我们一般会选择套用一些开源的Bootstrap 模板主题来进行前端设计.那如何套用呢?今天就简单创建一个ASP.NET Core Web MVC 模板项目为例, ...

  9. 【盛派周三分享-2019.2.20】开放分享内容,本期主题:《SCF、DDD及相关架构思想讨论》

    “周三分享”是盛派网络约定的每周三晚上定时举办的内部分享活动,活动主要由技术人员分享各方面的技术主题,并由所有参与者围绕主题进行讨论.除技术话题外,也可能涉及到相关的设计.财税.金融.政策等方面的延伸 ...

  10. 【从零开始搭建自己的.NET Core Api框架】(四)实战!带你半个小时实现接口的JWT授权验证

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...