c#解析json

json是一種通用的文本序列,有其固定的格式。 json的使用前必須轉(zhuǎn)化為實體類。轉(zhuǎn)化方法是在線轉(zhuǎn)換。網(wǎng)址:http://www.bejson.com/convert/json2csharp/

一,格式1及實體類

{"message":"ok","nu":"367847964498","ischeck":"1","condition":"F00","com":"shunfeng","status":"200","state":"3","data":[{"time":"2017-09-21 09:33:09","ftime":"2017-09-21 09:33:09","context":"已簽收,感謝使用順豐,期待再次為您服務(wù)","location":""},{"time":"2017-09-21 09:09:48","ftime":"2017-09-21 09:09:48","context":"快件交給鞏向濤,正在派送途中(聯(lián)系電話:18806439871)","location":""},{"time":"2017-09-21 07:02:41","ftime":"2017-09-21 07:02:41","context":"快件到達 【淄博市桓臺田莊速運營業(yè)點 】","location":""},{"time":"2017-09-20 15:32:00","ftime":"2017-09-20 15:32:00","context":"快件在【淄博市桓臺縣工業(yè)街營業(yè)點】已裝車,準(zhǔn)備發(fā)往下一站","location":""},{"time":"2017-09-20 13:37:08","ftime":"2017-09-20 13:37:08","context":"快件到達 【淄博市桓臺縣工業(yè)街營業(yè)點】","location":""},{"time":"2017-09-20 10:47:07","ftime":"2017-09-20 10:47:07","context":"快件在【淄博高新集散中心】已裝車,準(zhǔn)備發(fā)往下一站","location":""},{"time":"2017-09-20 10:15:47","ftime":"2017-09-20 10:15:47","context":"快件到達 【淄博高新集散中心】","location":""},{"time":"2017-09-19 23:20:18","ftime":"2017-09-19 23:20:18","context":"快件在【深圳總集散中心】已裝車,準(zhǔn)備發(fā)往下一站","location":""},{"time":"2017-09-19 22:39:27","ftime":"2017-09-19 22:39:27","context":"快件到達 【深圳總集散中心】","location":""},{"time":"2017-09-19 18:57:33","ftime":"2017-09-19 18:57:33","context":"快件在【深圳龍華新區(qū)華聯(lián)社區(qū)營業(yè)部】已裝車,準(zhǔn)備發(fā)往下一站","location":""},{"time":"2017-09-19 16:12:21","ftime":"2017-09-19 16:12:21","context":"順豐速運 已收取快件","location":""}]}

特征:

1,由大括號{}首尾包圍

2,包含兩大部分,第一部分為頭部,有一些屬性值。第二部分是正文,命名為data,由中括號[]包圍。

轉(zhuǎn)化為實體類:

public class DataItem{ /// /// /// public string time { get; set; }

/// ? ? ///? ? /// ? ? public string ftime { get; set; }

/// ? ? /// 已簽收,感謝使用順豐,期待再次為您服務(wù)? ? /// ? ? public string context { get; set; }

/// ? ? ///? ? /// ? ? public string location { get; set; }

}

public class Root{? ? /// ? ? ///? ? /// ? ? public string message { get; set; }

/// ? ? ///? ? /// ? ? public string nu { get; set; }

/// ? ? ///? ? /// ? ? public string ischeck { get; set; }

/// ? ? ///? ? /// ? ? public string condition { get; set; }

/// ? ? ///? ? /// ? ? public string com { get; set; }

/// ? ? ///? ? /// ? ? public string status { get; set; }

/// ? ? ///? ? /// ? ? public string state { get; set; }

/// ? ? ///? ? /// ? ? public List data { get; set; }

}

特征:

1,頭部類:Root,結(jié)尾處定義了其表示方法:data

2,主體類,從json中提取

二、格式2及實體類

{"log_id": 6822962530653775861, "words_result_num": 2, "words_result": [{"location": {"width": 317, "top": 32, "left": 0, "height": 43}, "words": "第一部分"}, {"location": {"width": 318, "top": 193, "left": 0, "height": 45}, "words": "第二部分"}]}

除具備格式1的特征外,發(fā)現(xiàn)主體部分命名為words_result_num,有兩個值分別是location和words,其第一個值又是一個新的集合。

public class Location{ /// /// /// public int width { get; set; }

/// ? ? ///? ? /// ? ? public int top { get; set; }

/// ? ? ///? ? /// ? ? public int left { get; set; }

/// ? ? ///? ? /// ? ? public int height { get; set; }

}

public class Words_resultItem{? ? /// ? ? ///? ? /// ? ? public Location location { get; set; }

/// ? ? /// 第一部分? ? /// ? ? public string words { get; set; }

}

public class Root{? ? /// ? ? ///? ? /// ? ? public int log_id { get; set; }

/// ? ? ///? ? /// ? ? public int words_result_num { get; set; }

/// ? ? ///? ? /// ? ? public List words_result { get; set; }

}

發(fā)現(xiàn),分成三個類。

二、添加一個類文件

在項目中添加一個類json1,使用轉(zhuǎn)換后的實體類,替換了:

class Json1

? ? {

? ? }

這時,無需引用,這個類文件即可被主程序調(diào)用。

三、聲明dll

下載?Newtonsoft.Json.dll,并聲明

using Newtonsoft.Json;

四、使用

格式1示例:

Root rt = JsonConvert.DeserializeObject<Root>(JsonTxt);

? ? ? ? ? ? MessageBox.Show("com=" + rt.com + "\r\n" + "condition=" + rt.condition + "\r\n" + "ischeck=" + rt.ischeck + "\r\n" + "state=" + rt.state + "\r\n" + "status=" + rt.status);

? ? ? ? ? ? for (int i = 0; i < rt.data.Count; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? MessageBox.Show("Data=" + rt.data[i].context + "\r\n" + rt.data[i].location + "\r\n" + rt.data[i].time + "\r\n" + rt.data[i].ftime);

? ? ? ? ? ? }

格式2示例:

Root rt = JsonConvert.DeserializeObject<Root>(getJsontxt);

? ? ? ? ? ? for (int i = 0; i < rt.words_result.Count; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? MessageBox.Show("Data=" + rt.words_result[i].words + "\r\n"+ rt.words_result[i].location.top);

? ? ? ? ? ? }

需要說明的是,示例2會報int32越界的錯誤,因為有一個整數(shù)型的值太大了,需要提前處理,將數(shù)值改小即可。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • JSON(全稱為JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式。它是基于Jav...
    littlewoodman閱讀 1,349評論 0 0
  • 最近需要在.net webserver中g(shù)et請求高德地圖的url,地理編碼獲取到地址的經(jīng)緯度,高德返回的是Jso...
    賈夢陽閱讀 1,692評論 0 0
  • JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式,和xml類似,本文主...
    KingOfLion閱讀 4,384評論 0 0
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,380評論 0 17
  • 杭州的雨 下的有點倉促 我一聲不吭 瞬間就將我雙眼模糊 昨夜的風(fēng) 不講情面的吹 刮走了心頭所有的苦 卻再也記不起曾...
    年幼的風(fēng)閱讀 613評論 2 1

友情鏈接更多精彩內(nèi)容