欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net实体类与json相互转换

程序员文章站 2023-11-27 09:37:04
.net实体类与json相互转换时,注意要点: 1.jsonhelp编写时候添加的引用。system.runtime.serialization.json; ...

.net实体类与json相互转换时,注意要点:
1.jsonhelp编写时候添加的引用。system.runtime.serialization.json; 
2.实体类需声明为public 

jsonhelp代码: 

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>();
    using (memorystream ms = new memorystream (encoding.utf8.getbytes(szjson)))
    {
      datacontractjsonserializer dcj = new datacontractjsonserializer(typeof(t));
      return (t)dcj.readobject(ms);
    }
  }
 

  }

} 

实体类代码: 

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; }
  }
} 

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

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
namespace jsontest

{
  class program

  {
    static void main(string[] 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();
    }

  }
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。