含有動態未知字段的 JSON 反序列化

通常來講,正常的 json 長這個模樣:json

{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}

 

這個時候咱們只須要創建對應的類,直接反序列化就行(好比宇宙第一VS的 「編輯」 -- 「選擇性粘貼」 -- 「將JSON粘貼爲類」)app

    public class Movie
    {
        public string Name { get; set; }
        public string ReleaseDate { get; set; }
        public string[] Genres { get; set; }
    }

    Movie m = JsonConvert.DeserializeObject<Movie>(json);

 

不過有的時候會有些不負責任的 JSONspa

有時候它長這樣:.net

{
	"mapping": [{
		"PHARMACOLOGY": "TU",
		"NEUROSCIENCES": "RU"
	}]
}

 

有時候它還長這樣:3d

{
	"mapping": [{
		"TELECOMMUNICATIONS": "YE"
	}]
}

 

這個時候序列化咱們就須要 字典 (Dictionary)code

這個時候咱們的類要長這樣:orm

    public class Rootobject
    {
        public List<Dictionary<string, string>> mapping { get; set; }
    }

 

而後照常反序列化:blog

Rootobject root = JsonConvert.DeserializeObject<Rootobject>(JSON);

 

循環下輸出結果:ci

                        i++;
                        foreach (var temp in root.mapping[0])
                        {
                            Console.WriteLine(String.Format("Number:{0},Keys:{1},Values:{2}", i, temp.Key, temp.Value));
                        }

 

結果以下:get

image

 

若是更麻煩一點的呢?

好比我在 https://blog.csdn.net/jdsjlzx/article/details/76785239 看到的這個json:

{
	"resultcode": "200",
	"reason": "successed!",
	"result": {
		"sk": {
			"temp": "24",
			"wind_direction": "東北風",
			"wind_strength": "2級",
			"humidity": "28%",
			"time": "17:38"
		},
		"today": {
			"temperature": "15℃~26℃",
			"weather": "多雲轉晴",
			"wind": "東北風微風",
			"week": "星期日",
			"city": "桂林",
			"date_y": "2015年10月11日",
			"dressing_index": "溫馨",
			"dressing_advice": "建議着長袖T恤、襯衫加單褲等服裝。年老體弱者宜着針織長袖襯衫、馬甲和長褲。",
			"uv_index": "",
			"comfort_index": "",
			"wash_index": "較適宜",
			"travel_index": "較適宜",
			"exercise_index": "較適宜",
			"drying_index": ""
		},
		"future": {
			"day_20151011": {
				"temperature": "15℃~26℃",
				"weather": "多雲轉晴",
				"wind": "東北風微風",
				"week": "星期日",
				"date": "20151011"
			},
			"day_20151012": {
				"temperature": "16℃~27℃",
				"weather": "晴轉多雲",
				"wind": "微風",
				"week": "星期一",
				"date": "20151012"
			},
			"day_20151013": {
				"temperature": "16℃~26℃",
				"weather": "多雲轉晴",
				"wind": "微風",
				"week": "星期二",
				"date": "20151013"
			},
			"day_20151014": {
				"temperature": "17℃~27℃",
				"weather": "",
				"wind": "北風微風",
				"week": "星期三",
				"date": "20151014"
			},
			"day_20151015": {
				"temperature": "17℃~28℃",
				"weather": "",
				"wind": "北風微風",
				"week": "星期四",
				"date": "20151015"
			},
			"day_20151016": {
				"temperature": "17℃~30℃",
				"weather": "",
				"wind": "北風微風",
				"week": "星期五",
				"date": "20151016"
			},
			"day_20151017": {
				"temperature": "17℃~30℃",
				"weather": "",
				"wind": "北風微風",
				"week": "星期六",
				"date": "20151017"
			}
		}
	},
	"error_code": 0
}

 

繼續使用 Dictionary 定義類:

    public class WeatherRootobject
    {
        public string resultcode { get; set; }
        public string reason { get; set; }
        public Result result { get; set; }
        public int error_code { get; set; }
    }

    public class Result
    {
        public Sk sk { get; set; }
        public Today today { get; set; }
        public Dictionary<string, Weather> future { get; set; }
    }

    public class Sk
    {
        public string temp { get; set; }
        public string wind_direction { get; set; }
        public string wind_strength { get; set; }
        public string humidity { get; set; }
        public string time { get; set; }
    }

    public class Today
    {
        public string temperature { get; set; }
        public string weather { get; set; }
        public string wind { get; set; }
        public string week { get; set; }
        public string city { get; set; }
        public string date_y { get; set; }
        public string dressing_index { get; set; }
        public string dressing_advice { get; set; }
        public string uv_index { get; set; }
        public string comfort_index { get; set; }
        public string wash_index { get; set; }
        public string travel_index { get; set; }
        public string exercise_index { get; set; }
        public string drying_index { get; set; }
    }

    public class Weather
    {
        public string temperature { get; set; }
        public string weather { get; set; }
        public string wind { get; set; }
        public string week { get; set; }
        public string date { get; set; }
    }

 

而後反序列化輸出:

            WeatherRootobject weathers = JsonConvert.DeserializeObject<WeatherRootobject>(json);
            foreach (var temp in weathers.result.future)
            {
                Console.WriteLine(String.Format("Day:{0},Week:{1},Temperature:{2}",  temp.Key, temp.Value.week, temp.Value.temperature));
            }

 

結果以下:

image

相關文章
相關標籤/搜索