Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 296|回复: 0

[默认分类] C# 中使用JSON - DataContractJsonSerializer

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-5-27 14:05:18 | 显示全部楼层 |阅读模式
    C#中使用JSON不需要使用第三方库,使用.NET Framwork3.5自带的System.Runtime.Serialization.Json即可很好的完成JSON的解析。
    关于JSON的入门介绍见(首页的图很形象):
    http://www.json.org/
      
    一、Using
    需要添加引用:System.ServiceModel.Web 和 System.Runtime.Serialization,然后使用Using:


    using
      System.Runtime.Serialization.Json;

    using
      System.Runtime.Serialization;

    二、定义序列化的类
    假如我们要转化的JSON字符串格式为:


    {
         
    "
    encoding
    "
    :
    "
    UTF-8
    "
    ,
         
    "
    plug-ins
    "
    :[
    "
    python
    "
    ,
    "
    c++
    "
    ,
    "
    ruby
    "
    ],
         
    "
    indent
    "
    :{
             
    "
    length
    "
    :
    3
    ,
             
    "
    use_space
    "
    :
    true

         }
    }

      
    然后编写相应的序列化的类,注意下面类加的Attribute:



    JSON Object
    [DataContract(Namespace = "http://coderzh.cnblogs.com")]
    class Config
    {
         [DataMember(Order = 0)]
         public string encoding { get; set; }
         [DataMember(Order = 1)]
         public string[] plugins { get; set; }
         [DataMember(Order = 2)]
         public Indent indent { get; set; }
    }

    [DataContract(Namespace = "http://coderzh.cnblogs.com")]
    class Indent
    {
         [DataMember(Order = 0)]
         public int length { get; set; }
         [DataMember(Order = 1)]
         public bool use_space { get; set; }
    }

    三、对象转化为JSON字符串
      
    使用WriteObject方法:
      



    WriteObject


      var config = new Config(){
                              encoding = "UTF-8",
                              plugins = new string[]{"python", "C++", "C#"},
                              indent = new Indent(){ length = 4, use_space = false}
                              };
    var serializer = new DataContractJsonSerializer(typeof(Config));
    var stream = new MemoryStream();
    serializer.WriteObject(stream, config);

    byte[] dataBytes = new byte[stream.Length];

    stream.Position = 0;

    stream.Read(dataBytes, 0, (int)stream.Length);

    string dataString = Encoding.UTF8.GetString(dataBytes);

    Console.WriteLine("JSON string is:");
    Console.WriteLine(dataString);

    四、JSON字符串转对象
    使用ReadObject方法:
      



    ReadObject


      var mStream = new MemoryStream(Encoding.Default.GetBytes(dataString));
    Config readConfig = (Config)serializer.ReadObject(mStream);

    Console.WriteLine("Encoding is: {0}", readConfig.encoding);
    foreach (string plugin in readConfig.plugins)
    {
         Console.WriteLine("plugins is: {0}", plugin);
    }
    Console.WriteLine("indent.length is: {0}", readConfig.indent.length);
    Console.WriteLine("indent.use_space is: {0}", readConfig.indent.use_space);

    五、输出结果:


    JSON
    string
      
    is
    :
    {
    "
    encoding
    "
    :
    "
    UTF-8
    "
    ,
    "
    plugins
    "
    :[
    "
    python
    "
    ,
    "
    C++
    "
    ,
    "
    C#
    "
    ],
    "
    indent
    "
    :{
    "
    length
    "
    :
    4
    ,
    "
    use_space
    "
    :
    false
    }}
    Encoding
    is
    : UTF
    -
    8

    plugins
    is
    : python
    plugins
    is
    : C
    ++

    plugins
    is
    : C#
    indent.length
    is
    :
    4

    indent.use_space
    is
    : False

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-25 16:52 , Processed in 0.366451 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表