C#處理Json字符串實(shí)例分析
Json字符串對(duì)于做web應(yīng)用的應(yīng)該很熟悉,其實(shí)在很多請(qǐng)求我們返回的都是Json字符串。那對(duì)于C#代碼如何處理Json字符串呢,.Net封裝了一個(gè)類叫做JavaScriptSerializer[MSDN Library 鏈接http://msdn.microsoft.com/en-us/library/ee191864(v=vs.110).aspx];這個(gè)類提供了一個(gè)方法。
下面這個(gè)是我在快遞100往抓取的一個(gè)圓通的快遞信息。對(duì)于我們有用的信息是快遞時(shí)間,快遞狀況。那我該如何來做。
{"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è)類,取名為PostalDeliveryModel,類名的結(jié)構(gòu)需要與Json結(jié)構(gòu)對(duì)應(yīng),名稱需要保持一樣[忽略大小寫],其次對(duì)應(yīng)的字段說會(huì)自動(dòng)轉(zhuǎn)換類型的,類型如果不符合會(huì)拋出異常
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.對(duì)象什么好后只需要調(diào)用方法即可:
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.方法回顧,雖然獲取到了。不過這種方法類的Public屬性名稱必須與Json字符串對(duì)應(yīng),不知道可否通過在Public屬性的上面加上[標(biāo)簽]來映射,這樣可以自定義名稱,不再需要與Json里面名稱一樣。求其他大牛在評(píng)論的時(shí)候指點(diǎn)一下。
以上所述就是對(duì)于c#如何處理json字符串的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
C#結(jié)合數(shù)據(jù)庫(kù)的數(shù)據(jù)采集器示例
這篇文章主要介紹了C#結(jié)合數(shù)據(jù)庫(kù)的數(shù)據(jù)采集器,功能比較實(shí)用,需要的朋友可以參考下2014-07-07
C#中Hashtable和Dictionary的區(qū)別
Hashtable 和 Dictionary 都是 C# 中用于存儲(chǔ)鍵值對(duì)的數(shù)據(jù)結(jié)構(gòu),本文主要介紹了C#中Hashtable和Dictionary的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
C# WPF 通過委托實(shí)現(xiàn)多窗口間的傳值的方法
這篇文章主要介紹了C# WPF 通過委托實(shí)現(xiàn)多窗口間的傳值的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
c#實(shí)現(xiàn)數(shù)據(jù)庫(kù)事務(wù)示例分享
這篇文章主要介紹了c#執(zhí)行多條sql更新語(yǔ)句實(shí)現(xiàn)數(shù)據(jù)庫(kù)事務(wù)的示例,大家參考使用吧2014-01-01
C# Lambda表達(dá)式select()和where()的區(qū)別及用法
這篇文章主要介紹了C# Lambda表達(dá)式select()和where()的區(qū)別及用法,select在linq中一般會(huì)用來提取最后篩選的元素集合,在lambda表達(dá)式中通常用where得到元素集合,需要的朋友可以參考下2023-07-07

