JSON 数据转换
- JSON概述
|
- .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 数据转换的更多相关文章
- 【转】C#中将JSon数据转换成实体类,将实体类转换成Json
http://wo13145219.iteye.com/blog/2022667 http://json2csharp.chahuo.com/ using System; using System.C ...
- Json数据与Json数据转换
1.json数据 [{\"IS_DISTRIBUTOR_LIMIT\":0,\"PROVISION_PRICE\":null,\"PRO_STATUS ...
- 利用JAVA反射机制将JSON数据转换成JAVA对象
net.sf.json.JSONObject为我们提供了toBean方法用来转换为JAVA对象, 功能更为强大, 这里借鉴采用JDK的反射机制, 作为简单的辅助工具使用, 有些数据类型需要进行转 ...
- VisualStudio2012轻松把JSON数据转换到POCO的代码
原文:VisualStudio2012轻松把JSON数据转换到POCO的代码 在Visual Studio 2012中轻松把JSON数据转换到POCO的代码,首先你需要安装Web Esse ...
- VisualStudio2012轻松把JSON数据转换到POCO的代码(转)
VisualStudio2012轻松把JSON数据转换到POCO的代码 在Visual Studio 2012中轻松把JSON数据转换到POCO的代码,首先你需要安装Web Essentials 20 ...
- JSON数据转换到POCO的代码
转载:http://www.cnblogs.com/wintersun/archive/2012/09/14/2684708.html 在Visual Studio 2012中轻松把JSON数据转换到 ...
- json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 执行:JSONArray arr ...
- 递归系列——树型JSON数据转换问题
JSON数据转换方式: 1.标准结构=>简单结构 var root = { id: 'root', children: [ { id: "1", children: [ { ...
- 将JSON数据转换成JAVA的实体类
思路:首先将JSON格式的数据转换成JSONObject,然后将JSONObject转换成Java的实体类(其中类属性包括List等类型) Java实体类: SearchFilter 类 1 publ ...
随机推荐
- java课程课后作业190425之一维数组最大子数组(界面实现)
题目要求: 1.在第一个问题过程中,我在以前的代码中好像已经写出了这个功能,想要实现这个功能,我们只需要在我们储存的数组和是负数的时候对中转值进行重新赋值就可以得到新的数值的起始位置,而他的终了位置就 ...
- SELECT 语句
常见表的操作 查看数据库的表 show table 查看表结构 desc 表名 删除表 drop table表 修改表的结构 添加列 alter table 表名 add 列名 ...
- JS HTML DOM代码(1)
<!DOCTYPE html> <html> <style type="text/css"> #容器 { width: 400px; heigh ...
- HBuilder git合作-上传项目到Git Hub
1.初始项目的创建 这里假设你已经在Git Hub上面建立好了代码的远程仓库,并已经邀请好了队员 在HBuidler中创建好初始的项目,然后右键,"Team"->" ...
- [Swift]LeetCode18. 四数之和 | 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- [Swift]LeetCode230. 二叉搜索树中第K小的元素 | Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Python档案袋( 面向对象 )
类即是一个模型,根据模型建立起不同的对象,对象间拥有共同的一些属性 简单的类: class P: #类变量,所有实例共享变量,推荐使用方法是:类名.类变量名 pvarx="ppvar1&qu ...
- hdfs创建删除文件和文件夹
在 hadoop 中,基于 Linux 命令可以给 hdfs 创建文件和文件夹,或者删除文件和文件夹 创建文件的命令为: hadoop fs -touch /file.txt 创建文件夹的命令为: h ...
- 【Redis篇】Redis集群安装与初始
一.前述 本文将单台节点不同端口模拟集群方式. 二.具体搭建 前提是安装好redis具体可参考http://www.cnblogs.com/LHWorldBlog/p/8463269.html 1 ...
- qt实现头像上传功能
想必大家都使用过qt的自定义头像功能吧,那么图1应该不会陌生,本片文章我就是要模拟一个这样的功能,虽然没有这么强大的效果,但是能够满足一定的需求. 图1 qq上传图片 首先在讲解功能之前,我先给出一片 ...