JSON数据的两种读写方法

简介: 1、利用.NET自身的JavaScriptSerializer 需要添加System.Web.Extensions.dll 添加方法见: http://blog.

1、利用.NET自身的JavaScriptSerializer

需要添加System.Web.Extensions.dll

添加方法见:

http://blog.chinaunix.net/uid-25498312-id-5675200.html


点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Script.Serialization;
  6. namespace TestJSON
  7. {
  8.     class CustomData
  9.     {
  10.         public string Input;
  11.         public string Output;
  12.     }
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Console.WriteLine("----------------------- Using .Net JavaScriptSerializer api for JSON ------------------------\n");
  18.             CustomData p = new CustomData() { Input = "stone", Output = "gold" };
  19.             JavaScriptSerializer serializer = new JavaScriptSerializer();
  20.             var json = serializer.Serialize(p);
  21.             Console.WriteLine(json);
  22.             var p1 = serializer.Deserialize(json);
  23.             Console.WriteLine(p1.Input + "=>" + p1.Output);
  24.             // 确定指定的 System.Object 实例是否是相同的实例
  25.             Console.WriteLine(ReferenceEquals(p, p1));
  26.             Console.ReadLine();
  27.         }
  28.     }
  29. }

image

上述结果同时证明了从p到p1是深拷贝。


2、利用Newtonsoft.Json.dll开源库

点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Script.Serialization;
  6. namespace TestJSON
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("----------------------- Using NewtonSoftJson api for JSON ------------------------\n");
  13.             //匿名对象解析,uid=0即为整型,若uid="0"则为字符串 
  14.             var tempEntity = new { uid = 0, rid = 0, cmd = 0, commander = 0, target = 0 };
  15.             // 序列化的后发送
  16.             string jsonStr = JsonHelper.SerializeObject(tempEntity);
  17.             // 收到后解析
  18.             tempEntity = JsonHelper.DeserializeAnonymousType("{\"uid\":123,\"rid\":466,\"cmd\":4099,\"commander\":123,\"target\":666}", tempEntity);
  19.             Console.WriteLine(tempEntity.uid);
  20.             Console.ReadLine();
  21.         }
  22.     }
  23. }


  1. /* JasonHelper.cs文件: */
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7. using Newtonsoft.Json;

  8. namespace TestJSON
  9. {
  10.     ///
  11.     /// Json帮助类
  12.     ///
  13.     public class JsonHelper
  14.     {
  15.         ///
  16.         /// 将对象序列化为JSON格式
  17.         ///
  18.         /// 对象
  19.         /// json字符串
  20.         public static string SerializeObject(object o)
  21.         {
  22.             string json = JsonConvert.SerializeObject(o);
  23.             return json;
  24.         }

  25.         ///
  26.         /// 解析JSON字符串生成对象实体
  27.         ///
  28.         /// 对象类型
  29.         /// json字符串(eg.{"ID":"112","Name":"石子儿"})
  30.         /// 对象实体
  31.         public static T DeserializeJsonToObject(string json) where T : class
  32.         {
  33.             JsonSerializer serializer = new JsonSerializer();
  34.             StringReader sr = new StringReader(json);
  35.             object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
  36.             T t = o as T;
  37.             return t;
  38.         }


  39.         ///
  40.         /// 解析JSON数组生成对象实体集合
  41.         ///
  42.         /// 对象类型
  43.         /// json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])
  44.         /// 对象实体集合
  45.         public static List DeserializeJsonToList(string json) where T : class
  46.         {
  47.             JsonSerializer serializer = new JsonSerializer();
  48.             StringReader sr = new StringReader(json);
  49.             object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List));
  50.             List list = o as List;
  51.             return list;
  52.         }

  53.         ///
  54.         /// 反序列化JSON到给定的匿名对象.
  55.         ///
  56.         /// 匿名对象类型
  57.         /// json字符串
  58.         /// 匿名对象
  59.         /// 匿名对象
  60.         public static T DeserializeAnonymousType(string json, T anonymousTypeObject)
  61.         {
  62.             T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
  63.             return t;
  64.         }
  65.     }
  66. }


image


工程源码:
img_e25d4fb2f8de1caf41a735ec53088516.pngTestJSON.rar
Newtonsoft.Json.dll库:
img_e25d4fb2f8de1caf41a735ec53088516.pngNewtonsoft.rar

参考文献:

http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-json.html

相关文章
|
16天前
|
JSON JavaScript 前端开发
JavaScript原生代码处理JSON的一些高频次方法合集
JavaScript原生代码处理JSON的一些高频次方法合集
|
1月前
|
存储 JSON Apache
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
在最新发布的阿里云数据库 SelectDB 的内核 Apache Doris 2.1 新版本中,我们引入了全新的数据类型 Variant,对半结构化数据分析能力进行了全面增强。无需提前在表结构中定义具体的列,彻底改变了 Doris 过去基于 String、JSONB 等行存类型的存储和查询方式。
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
|
1月前
|
存储 JSON JavaScript
Python字典和JSON字符串相互转化方法
【2月更文挑战第18天】
59 3
|
12天前
|
存储 JSON JavaScript
「Python系列」Python JSON数据解析
在Python中解析JSON数据通常使用`json`模块。`json`模块提供了将JSON格式的数据转换为Python对象(如列表、字典等)以及将Python对象转换为JSON格式的数据的方法。
28 0
|
16天前
|
存储 JSON 数据挖掘
python逐行读取txt文本中的json数据,并进行处理
Python代码示例演示了如何读取txt文件中的JSON数据并处理。首先,逐行打开文件,然后使用`json.loads()`解析每一行。接着,处理JSON数据,如打印特定字段`name`。异常处理包括捕获`JSONDecodeError`和`KeyError`,确保数据有效性和字段完整性。将`data.txt`替换为实际文件路径运行示例。
14 2
|
1月前
|
JSON 数据格式
糊涂工具类(hutool)post请求设置body参数为json数据
糊涂工具类(hutool)post请求设置body参数为json数据
40 1
|
1月前
|
JSON 前端开发 数据格式
Ajax传递json数据
Ajax传递json数据
11 0
|
1月前
|
JSON 并行计算 API
使用CJSON/Nlohmann:快速简便地在C/C++中处理JSON数据
使用CJSON/Nlohmann:快速简便地在C/C++中处理JSON数据
120 0
|
1月前
|
JSON 数据处理 API
盘点Python中4种读取JSON文件和提取JSON文件内容的方法
盘点Python中4种读取JSON文件和提取JSON文件内容的方法
300 0
|
1月前
|
存储 JSON JavaScript
Python中读写(解析)JSON文件的深入探究
Python中读写(解析)JSON文件的深入探究
32 0