JSON 数据转换
- JSON概述
JSON(Java Script Object Notation)JS对象符号,通常JSON和XML是二选一的,JSON的数据格式很类似于JavaScript的对象
|
- .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组件方式进行数据转换
-
- 下载地址
-
- 简易方式
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;
}
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】