旗下导航:搜·么
当前位置:网站首页 > .Net教程 > 正文

C#怎样完成JSON与对象之间相互转换功用示例【C#.Net教程】,javascript,csharp,.net

作者:搜教程发布时间:2019-11-27分类:.Net教程浏览:20评论:0


导读:这篇文章重要引见了C#完成JSON和对象之间互相转换功用,连系实例情势较为细致的剖析了C#完成对象与json之间互相转换的操纵技能,须要的朋侪能够参考下本文实例报告了C#完...
这篇文章重要引见了C#完成JSON和对象之间互相转换功用,连系实例情势较为细致的剖析了C#完成对象与json之间互相转换的操纵技能,须要的朋侪能够参考下

本文实例报告了C#完成JSON和对象之间互相转换功用。分享给人人供人人参考,详细以下:

1.首先是声明用户信息对象,DataContract润饰类,示意能够被剖析成JSON,DataMember润饰属性,Order示意 剖析的递次,别的Lover是数组列表,示意女朋侪个数

Address 示意送货地点,DailyRecord 示意一样平常纪录


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace FunctionTest.Model
{
  [DataContract]
  public class UserInfo
  {
    [DataMember(Order =0)]
    public string UserName { get; set; }
    [DataMember(Order = 1)]
    public int Age { get; set; }
    [DataMember(Order = 2)]
    public int Gender { get; set; }
    [DataMember(Order =3)]
    public List<string> Lover { get; set; }
    [DataMember(Order = 4)]
    public ContactAddress Address { get; set; }
    [DataMember(Order = 5)]
    public Dictionary<string, string> DailyRecord {
      get; set;
    }
  }
  [DataContract]
  public class ContactAddress
  {
    [DataMember(Order =0)]
    public string Province { get; set; }
    [DataMember(Order = 1)]
    public string City { get; set; }
    [DataMember(Order = 2)]
    public string Country { get; set; }
    [DataMember(Order = 3)]
    public string Details { get; set; }
  }
}

2.JSON协助类 中心代码


/// <summary>
/// Json转换成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonText"></param>
/// <returns></returns>
public static T JsonToObject<T>(string jsonText)
{
  DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
  MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));
  T obj = (T)s.ReadObject(ms);
  ms.Dispose();
  return obj;
}
/// <summary>
/// 对象转换成JSON
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string ObjectToJSON<T>(T obj)
{
  DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
  string result = string.Empty;
  using (MemoryStream ms = new MemoryStream())
  {
    serializer.WriteObject(ms, obj);
    ms.Position = 0;
    using (StreamReader read = new StreamReader(ms))
    {
      result = read.ReadToEnd();
    }
  }
  return result;
}

3.挪用


//1.对象-->JSON
UserInfo info = new UserInfo
{
    Age = 10,
    Gender = 1,
    UserName = "刘德华",
    Lover = new List<string> { "玉人1", "玉人2", "玉人3" },
    Address = new ContactAddress
    {
      Province = "湖南省",
      City = "长沙市",
      Country = "望城县",
      Details = "某旮旯快递找不到的处所"
    },
    DailyRecord = new Dictionary<string, string> { { "星期一", "用饭" }, { "星期二", "洗衣服" }, { "星期三", "好事情" } }
};
string json = ObjectToJSON<UserInfo>(info);

4.反序列化后的效果

代码以下:

{"UserName":"刘德华","Age":10,"Gender":1,"Lover":["玉人1","玉人2","玉人3"],"Address":{"Province":"湖南省","City":"长沙市","Country":"望城县",
"Details":"某旮旯快递找不到的处所"},"DailyRecord":[{"Key":"星期一","Value":"用饭"},{"Key":"星期二","Value":"洗衣服"},{"Key":"星期三","Value":"好事情"}]}

以上就是C#怎样完成JSON与对象之间互相转换功用示例的细致内容,更多请关注ki4网别的相干文章!

标签:javascriptcsharp.net


欢迎 发表评论: