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

所属分类: 软件编程 / C#教程 阅读数: 90
收藏 0 赞 0 分享

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

一般对于web应用开发人员来说对Json字符串都会很熟悉,其实在很多请求我们返回的都是Json字符串。那对于C#代码如何处理Json字符串呢,.Net封装了一个类叫做JavaScriptSerializer[MSDN Library 链接:http://msdn.microsoft.com/en-us/library/ee191864(v=vs.110).aspx];这个类提供了一个方法。

下面这个是在快递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#程序设计的学习有所帮助。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多