欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#處理Json字符串實(shí)例分析

 更新時(shí)間:2015年02月22日 09:25:38   投稿:hebedich  
這篇文章主要通過(guò)具體的實(shí)例,向我們分析了C#如何處理Json字符串,需要的朋友可以參考下

Json字符串對(duì)于做web應(yīng)用的應(yīng)該很熟悉,其實(shí)在很多請(qǐng)求我們返回的都是Json字符串。那對(duì)于C#代碼如何處理Json字符串呢,.Net封裝了一個(gè)類(lèi)叫做JavaScriptSerializer[MSDN Library 鏈接http://msdn.microsoft.com/en-us/library/ee191864(v=vs.110).aspx];這個(gè)類(lèi)提供了一個(gè)方法。

          下面這個(gè)是我在快遞100往抓取的一個(gè)圓通的快遞信息。對(duì)于我們有用的信息是快遞時(shí)間,快遞狀況。那我該如何來(lái)做。 

復(fù)制代碼 代碼如下:

{"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":"甘肅省蘭州市公司 已發(fā)出 ","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字符串結(jié)構(gòu). Json{ message,nu,isCheck,Data{time,context,ftime}};我們先定義一個(gè)類(lèi),取名為PostalDeliveryModel,類(lèi)名的結(jié)構(gòu)需要與Json結(jié)構(gòu)對(duì)應(yīng),名稱(chēng)需要保持一樣[忽略大小寫(xiě)],其次對(duì)應(yīng)的字段說(shuō)會(huì)自動(dòng)轉(zhuǎn)換類(lèi)型的,類(lèi)型如果不符合會(huì)拋出異常

復(fù)制代碼 代碼如下:

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字符串相同,但是忽略大小寫(xiě)
        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.對(duì)象什么好后只需要調(diào)用方法即可:

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web.Script.Serialization; // 此命名空間對(duì)應(yīng)的框架是 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.運(yùn)行監(jiān)控model對(duì)象.數(shù)據(jù)已經(jīng)在對(duì)象里面了。

    4.方法回顧,雖然獲取到了。不過(guò)這種方法類(lèi)的Public屬性名稱(chēng)必須與Json字符串對(duì)應(yīng),不知道可否通過(guò)在Public屬性的上面加上[標(biāo)簽]來(lái)映射,這樣可以自定義名稱(chēng),不再需要與Json里面名稱(chēng)一樣。求其他大牛在評(píng)論的時(shí)候指點(diǎn)一下。

以上所述就是對(duì)于c#如何處理json字符串的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論