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ù)值改小即可。