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

C#实现对Json字符串处理实例

程序员文章站 2023-12-19 15:29:16
本文实例讲述了c#实现对json字符串处理方法,分享给大家供大家参考。具体分析如下: 一般对于web应用开发人员来说对json字符串都会很熟悉,其实在很多请求我们返回的都...

本文实例讲述了c#实现对json字符串处理方法,分享给大家供大家参考。具体分析如下:

一般对于web应用开发人员来说对json字符串都会很熟悉,其实在很多请求我们返回的都是json字符串。那对于c#代码如何处理json字符串呢,.net封装了一个类叫做javascriptserializer[msdn library 链接:];这个类提供了一个方法。

下面这个是在快递100往抓取的一个圆通的快递信息。对于我们有用的信息是快递时间,快递状况。那我该如何来做。 

{"message":"ok","nu":"9356359685","ischeck":"1","com":"yuantong","status":"200","condition":"f00","state":"3","data":[{"time":"2014-09-01 21:19:06","context":"甘肃省武威市公司 已签收 ","ftime":"2014-09-01 21:19:06"},{"time":"2014-09-01 09:09:28","context":"甘肃省武威市公司 派件中 ","ftime":"2014-09-01 09:09:28"},{"time":"2014-09-01 09:06:27","context":"甘肃省武威市公司 已收入 ","ftime":"2014-09-01 09:06:27"},{"time":"2014-08-31 19:53:47","context":"甘肃省兰州市公司 已发出 ","ftime":"2014-08-31 19:53:47"},{"time":"2014-08-31 19:17:41","context":"甘肃省兰州市公司 已收入 ","ftime":"2014-08-31 19:17:41"},{"time":"2014-08-28 23:44:26","context":"广东省深圳市横岗公司 已打包 ","ftime":"2014-08-28 23:44:26"},{"time":"2014-08-28 23:19:12","context":"广东省深圳市横岗公司 已收件 ","ftime":"2014-08-28 23:19:12"},{"time":"2014-08-28 21:55:35","context":"广东省深圳市横岗公司 已收件 ","ftime":"2014-08-28 21:55:35"}]}

 
1. 首先分析json字符串结构. json{ message,nu,ischeck,data{time,context,ftime}};我们先定义一个类,取名为postaldeliverymodel,类名的结构需要与json结构对应,名称需要保持一样[忽略大小写],其次对应的字段说会自动转换类型的,类型如果不符合会抛出异常

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

namespace testjson
{
  public class postaldeliverymodel
  {
    private string message = string.empty;
    private string nu = string.empty;
    private list<singaldata> data = new list<singaldata>();

    // puclic的名字需要与json字符串相同,但是忽略大小写
    public string message 
    {
      get { return this.message; }
      set { this.message = value; }
    }

    public string nu
    {
      get { return this.nu; }
      set { this.nu = value; }
    }

    public list<singaldata> data
    {
      get { return this.data; }
      set { this.data = value; }
    }
  }

  public class singaldata
  {
    private datetime time = system.datetime.now;
    private string context = string.empty;
    private datetime ftime = system.datetime.now;

    public datetime time
    {
      get { return this.time; }
      set { this.time = value; }
    }

    public datetime ftime
    {
      get { return this.ftime; }
      set { this.ftime = value; }
    }

    public string context
    {
      get { return this.context; }
      set { this.context = value; }
    }
  }
}

2.对象什么好后只需要调用方法即可:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.io;
using system.web.script.serialization; // 此命名空间对应的框架是 system.web.extensions

namespace testjson
{
  public class program
  {
    public static void main(string[] args)
    {
      string jsonstr = new streamreader("jsondata.txt").readtoend();
      postaldeliverymodel mode = new javascriptserializer().deserialize<postaldeliverymodel>(jsonstr);
      console.readkey();
    }
  }
}

3.运行监控model对象.数据已经在对象里面了。

4.方法回顾,虽然获取到了。不过这种方法类的public属性名称必须与json字符串对应,不知道可否通过在public属性的上面加上[标签]来映射,这样可以自定义名称,不再需要与json里面名称一样。感兴趣的朋友可以对此进一步研究一下!

希望本文所述对大家c#程序设计的学习有所帮助。

上一篇:

下一篇: