Home  >  Article  >  Web Front-end  >  Summary of conversion methods between .net entity classes and json

Summary of conversion methods between .net entity classes and json

php中世界最好的语言
php中世界最好的语言Original
2018-04-25 13:41:152105browse

This time I will bring you a summary of the conversion methods between .net entity classes and json. What are the precautions for converting .net entity classes and json. The following is a practical case, let's take a look.

When converting between .net entity classes and json, pay attention to the following points:

1. References added when writing jsonhelp. System.Runtime.Serialization.Json;
2. The entity class needs to be declared as public

jsonhelp code:

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
    //
  }
  /// 
  /// 把对象序列化 JSON 字符串 
  /// 
  /// 对象类型
  /// 对象实体
  /// JSON字符串
  public static string GetJson(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;
    }
  }
  /// 
  /// 把JSON字符串还原为对象
  /// 
  /// 对象类型
  /// JSON字符串
  /// 对象实体
  public static T ParseFormJson(string szJson)
  {
    T obj = Activator.CreateInstance();
    using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes(szJson)))
    {
      DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
      return (T)dcj.ReadObject(ms);
    }
  }
 
  }
}

Entity class code:

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

Console application test code:

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 tlist = new List();
      tlist.Add(t1);
      tlist.Add(t2);
      tlist.Add(t3);
     Console.WriteLine(JsonHelp.GetJson>(tlist));
      // Console.ReadKey();
      //json转实体类
     List tl = JsonHelp.ParseFormJson >(JsonHelp.GetJson>(tlist));
     Console.WriteLine(tl.Count);
     Console.WriteLine(tl[0].Name);
     Console.ReadKey();
    }
  }
}

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website !

Recommended reading:

jQuery creates a vertical translucent accordion effect

jquery implements the navigation menu mouse prompt function

The above is the detailed content of Summary of conversion methods between .net entity classes and json. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn