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 ...
随机推荐
- 做个流量站-聚茶吧, 汇聚"茶"的地方
犹豫了好久,终于下定决心,做一回个人站长了,虽然没啥经验,但毕竟也是IT科班出身了,准备用一年的事件摸索一下内容站和SEO,看看能否积累点经验,赚点小钱. 推酷-靠爬虫起家的内容站 做内容站,站长们都 ...
- python | Elasticsearch-dsl常用方法总结(join为案例)
Elasticsearch DSL是一个高级库,其目的是帮助编写和运行针对Elasticsearch的查询.它建立在官方低级客户端(elasticsearch-py)之上. 它提供了一种更方便和习惯的 ...
- Git生成ssh密钥指定文件
ssh-keygen 使用的时候可以直接使用 -f 参数 指定密钥保存文件,省去后面生成成功后再提示选择保存文件: ssh-keygen -t rsa -C "abc@example.com ...
- DOS命令(二)
1. findstr “要查找的字符串” 文件,用来从文件中检索包含相关内容的字符串集合. [例如:查找包含“TTL”的字符串] 2. del 要删除的文件,用来删除某个文件. 3. pause,用 ...
- react-native-upgrade-android
React Native的版本升级插件(仅是android), react-native版本需要0.17.0及以上 如何安装 1.首先安装npm包 npm install react-native-u ...
- github pages + Hexo + node.js 搭建属于自己的个人博客网站
之前我写过一篇用Github实现个人主页的博客:https://www.cnblogs.com/tu-0718/p/8081288.html 后来看到某个大佬写的文章:[5分钟 0元搭建个人独立 ...
- [Swift]LeetCode33. 搜索旋转排序数组 | Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [Swift]LeetCode560. 和为K的子数组 | Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- HBase之Table.put客户端流程(续)
上篇博文中已经谈到,有两个流程没有讲到.一个是MetaTableAccessor.getRegionLocations,另外一个是ConnectionImplementation.cacheLocat ...
- 「造个轮子」——设计 HTTP 请求全局上下文
前言 本次 Cicada 已经更新到了 v1.0.3. 主要是解决了两个 issue,#9(Boss线程数好像设置有误 ) #8(怎么返回纯字符串内容不要JSON格式?). 所以本次的主要更新为: C ...