分享

.net实体类与json互相转换方法汇总

 mjsws 2018-04-25
这次给大家带来.net实体类与json互相转换方法汇总,.net实体类与json互相转换的注意事项有哪些,下面就是实战案例,一起来看一下。

.net实体类与json相互转换时,注意要点:

1.jsonhelp编写时候添加的引用。System.Runtime.Serialization.Json; 
2.实体类需声明为public

jsonhelp代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
using System.IO;
namespace JsonTest
{
  class JsonHelp
  {
    public JsonHelp()
  {
    //
    // TODO: Add constructor logic here
    //
  }
  /// <summary>
  /// 把对象序列化 JSON 字符串
  /// </summary>
  /// <typeparam name="T">对象类型</typeparam>
  /// <param name="obj">对象实体</param>
  /// <returns>JSON字符串</returns>
  public static string GetJson<T>(T obj)
  {
    //记住 添加引用 System.ServiceModel.Web
    /**
     * 如果不添加上面的引用,System.Runtime.Serialization.Json; Json是出不来的哦
     * */
    DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
    using (MemoryStream ms = new MemoryStream())
    {
      json.WriteObject(ms, obj);
      string szJson = Encoding.UTF8.GetString(ms.ToArray());
      return szJson;
    }
  }
  /// <summary>
  /// 把JSON字符串还原为对象
  /// </summary>
  /// <typeparam name="T">对象类型</typeparam>
  /// <param name="szJson">JSON字符串</param>
  /// <returns>对象实体</returns>
  public static T ParseFormJson<T>(string szJson)
  {
    T obj = Activator.CreateInstance<T>(www.jiekeqipai.net);
    using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes(szJson)))
    {
      DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
      return (T)dcj.ReadObject(ms);
    }
  }
  
  }
}

实体类代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JsonTest
{
 public class testData
  {
    public testData()
  {
  }
  public int Id { get; set; }
  public string Name { get; set; }
  public string Sex { get; set; }
  }
}

控制台应用程序测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JsonTest
{
  class Program
  {
    static void Main(string[www.rodlg.com] args)
    {
      //实体类转json
      testData t1 = new testData();
      t1.Id = 1;
      t1.Name = "001姓名";
      t1.Sex = "男";
      testData t2 = new testData();
      t2.Id = 2;
      t2.Name = "002姓名";
      t2.Sex = "男";
      testData t3 = new testData();
      t3.Id = 3;
      t3.Name = "003姓名";
      t3.Sex = "男";
      List<testData> tlist = new List<testData>();
      tlist.Add(t1);
      tlist.Add(t2);
      tlist.Add(t3);
     Console.WriteLine(JsonHelp.GetJson<List<testData>>(tlist));
      // Console.ReadKey();
      //json转实体类
     List<testData> tl = JsonHelp.ParseFormJson <List<testData>>(JsonHelp.GetJson<List<testData>>(tlist));
     Console.WriteLine(tl.Count);
     Console.WriteLine(tl[0].Name);
     Console.ReadKey();
    }
  }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多