• 含有动态未知字段的 JSON 反序列化


    一般来说,正常的 json 长这个模样:

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

    这个时候我们只需要建立对应的类,直接反序列化就行(比如宇宙第一VS的 “编辑” -- “选择性粘贴” -- “将JSON粘贴为类”)

    复制代码
        public class Movie
        {
            public string Name { get; set; }
            public string ReleaseDate { get; set; }
            public string[] Genres { get; set; }
        }
    
        Movie m = JsonConvert.DeserializeObject<Movie>(json);
    复制代码

    不过有的时候会有些不负责任的 JSON

    有时候它长这样:

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

    有时候它还长这样:

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

    这个时候序列化我们就需要 字典 (Dictionary)

    这个时候我们的类要长这样:

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

    然后照常反序列化:

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

    循环下输出结果:

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

    结果如下:

    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

    作者:sun8134
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    学习笔记:javascript 窗口对象(window)
    学习笔记:javascript内置对象:数组对象
    学习笔记:javascript内置对象:日期对象
    学习笔记:javascript内置对象:数学对象
    学习笔记:javascript内置对象:字符串对象
    初次尝试轮播图
    今天我开通了博客园
    javaweb项目运转流程
    idea tomcat启动无效
    mysql事务管理及spring声明式事务中主动异常抛出使数据库回滚
  • 原文地址:https://www.cnblogs.com/Alex80/p/14982558.html
Copyright © 2020-2023  润新知