往往會有這么一種情景:有一個自定義的json或者json數組的字符串,希望轉換成對應的對象。由於程序對於自定義的JSON格式並不清楚,所以直接ToObject()或者ToList()均會失敗。
這時我們要做的就是讓程序知道我們自定義的json到底是什么樣的。如下:
//數據庫中某個字段內容如下,NeedToJson用於接收該字段值 // [{"ProjectName":"鍋爐整體是否有明顯的變形、保溫是否完好","ShowType":0,"IsMust":1},{"ProjectName":"鍋爐煤量(噸)","ShowType":1,"IsMust":1},{"ProjectName":"鍋爐圖片","ShowType":2,"IsMust":1}] public class NeedToJsonDbo { //省略部分代碼。。。 string NeedToJson= 數據庫操作,接受 NeedToJson 字段值; List<ToJsonDetailJson> strToJson = NeedToJson.ToList<ToJsonDetailJson>(); //省略部分代碼。。。 } //聲明一個自定義json內容一致的類 public class ToJsonDetailJson { //屬性的名字,必須與json格式字符串中的"key"值一樣。 public string ProjectName { get; set; } public string ShowType { get; set; } public string IsMust { get; set; } }
以上在對象屬性均為簡單數據類型時沒有問題,但是當對象的某個數據仍未對象時就不行了。
這時可以引入 Newtonsoft.Json.Linq
例如請求國家氣象局獲得城市天氣接口獲得數據(接口如下)
http://t.weather.sojson.com/api/weather/city/
101120201
獲得的數據如下:
{ "data": { "message": "success感謝又拍雲(upyun.com)提供CDN贊助", "status": 200, "date": "20191129", "time": "2019-11-29 09:51:46", "cityInfo": { "city": "青島市", "citykey": "101120201", "parent": "山東", "updateTime": "09:09" }, "data": { "shidu": "63%", "pm25": 30, "pm10": 63, "quality": "良", "wendu": "1", "ganmao": "極少數敏感人群應減少戶外活動", "forecast": [ { "date": "29", "high": "高溫 6℃", "low": "低溫 2℃", "ymd": "2019-11-29", "week": "星期五", "sunrise": "06:47", "sunset": "16:45", "aqi": 51, "fx": "北風", "fl": "3-4級", "type": "多雲", "notice": "陰晴之間,謹防紫外線侵擾" }, { "date": "30", "high": "高溫 7℃", "low": "低溫 4℃", "ymd": "2019-11-30", "week": "星期六", "sunrise": "06:48", "sunset": "16:45", "aqi": 67, "fx": "北風", "fl": "3-4級", "type": "陣雨", "notice": "陣雨來襲,出門記得帶傘" }, { "date": "01", "high": "高溫 7℃", "low": "低溫 2℃", "ymd": "2019-12-01", "week": "星期日", "sunrise": "06:49", "sunset": "16:44", "aqi": 78, "fx": "北風", "fl": "3-4級", "type": "多雲", "notice": "陰晴之間,謹防紫外線侵擾" }, { "date": "02", "high": "高溫 5℃", "low": "低溫 -1℃", "ymd": "2019-12-02", "week": "星期一", "sunrise": "06:50", "sunset": "16:44", "aqi": 67, "fx": "北風", "fl": "5-6級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" }, { "date": "03", "high": "高溫 7℃", "low": "低溫 0℃", "ymd": "2019-12-03", "week": "星期二", "sunrise": "06:51", "sunset": "16:44", "aqi": 82, "fx": "西南風", "fl": "3-4級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" }, { "date": "04", "high": "高溫 8℃", "low": "低溫 1℃", "ymd": "2019-12-04", "week": "星期三", "sunrise": "06:52", "sunset": "16:44", "aqi": 78, "fx": "北風", "fl": "3-4級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" }, { "date": "05", "high": "高溫 7℃", "low": "低溫 0℃", "ymd": "2019-12-05", "week": "星期四", "sunrise": "06:53", "sunset": "16:44", "fx": "南風", "fl": "3-4級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" }, { "date": "06", "high": "高溫 6℃", "low": "低溫 0℃", "ymd": "2019-12-06", "week": "星期五", "sunrise": "06:54", "sunset": "16:44", "fx": "北風", "fl": "<3級", "type": "多雲", "notice": "陰晴之間,謹防紫外線侵擾" }, { "date": "07", "high": "高溫 5℃", "low": "低溫 0℃", "ymd": "2019-12-07", "week": "星期六", "sunrise": "06:54", "sunset": "16:44", "fx": "北風", "fl": "3-4級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" }, { "date": "08", "high": "高溫 6℃", "low": "低溫 1℃", "ymd": "2019-12-08", "week": "星期日", "sunrise": "06:55", "sunset": "16:44", "fx": "西北風", "fl": "3-4級", "type": "多雲", "notice": "陰晴之間,謹防紫外線侵擾" }, { "date": "09", "high": "高溫 8℃", "low": "低溫 3℃", "ymd": "2019-12-09", "week": "星期一", "sunrise": "06:56", "sunset": "16:44", "fx": "西風", "fl": "<3級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" }, { "date": "10", "high": "高溫 10℃", "low": "低溫 3℃", "ymd": "2019-12-10", "week": "星期二", "sunrise": "06:57", "sunset": "16:44", "fx": "西南風", "fl": "3-4級", "type": "多雲", "notice": "陰晴之間,謹防紫外線侵擾" }, { "date": "11", "high": "高溫 9℃", "low": "低溫 1℃", "ymd": "2019-12-11", "week": "星期三", "sunrise": "06:58", "sunset": "16:44", "fx": "西北風", "fl": "3-4級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" }, { "date": "12", "high": "高溫 7℃", "low": "低溫 1℃", "ymd": "2019-12-12", "week": "星期四", "sunrise": "06:58", "sunset": "16:44", "fx": "東北風", "fl": "<3級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" }, { "date": "13", "high": "高溫 7℃", "low": "低溫 2℃", "ymd": "2019-12-13", "week": "星期五", "sunrise": "06:59", "sunset": "16:45", "fx": "南風", "fl": "<3級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" } ], "yesterday": { "date": "28", "high": "高溫 5℃", "low": "低溫 0℃", "ymd": "2019-11-28", "week": "星期四", "sunrise": "06:46", "sunset": "16:45", "aqi": 39, "fx": "北風", "fl": "4-5級", "type": "晴", "notice": "願你擁有比陽光明媚的心情" } } }, "msg": "請求成功!", "code": 20000, "success": true }
using Newtonsoft.Json.Linq; //---省略部分代碼---// var data=請求國家氣象局接口獲得數據。如上。 var weatherData = JObject.Parse(data); var weatherData_data = weatherData["data"];//此時不能用'.'操作獲取到data屬性值,但是可以通過屬性名獲取屬性值。 //---省略部分代碼---//