现代操作系统应用开发:UWP——(C#)Newtonsoft.Json:一种方便的解析Json字符串的方法

功能简介

之前的一篇博客现代操作系统应用开发:网络编程(一):Json字符串和Xml字符串解析写过如何解析Json字符串,但是!!!今天发现使用Newtonsoft.Json相对于微软的Windows.Data.Json,可以更加简单的解析Json字符串。

实现过程

第一步,建立我们的Json对象

这里使用我们上次聚合数据的Json数据,可以直接复制下面的内容:

{
    "resultcode": "200",
    "reason": "查询成功!",
    "result": {
        "sk": { /*当前实况天气*/
            "temp": "21",   /*当前温度*/
            "wind_direction": "西风", /*当前风向*/
            "wind_strength": "2级",  /*当前风力*/    
            "humidity": "4%",   /*当前湿度*/
            "time": "14:25" /*更新时间*/
        },
        "today": {
            "city": "天津",
            "date_y": "2014年03月21日",
            "week": "星期五",
            "temperature": "8℃~20℃",   /*今日温度*/
            "weather": "晴转霾",   /*今日天气*/
            "weather_id": { /*天气唯一标识*/
                "fa": "00", /*天气标识00:晴*/
                "fb": "53"  /*天气标识53:霾 如果fa不等于fb,说明是组合天气*/
            },
            "wind": "西南风微风",
            "dressing_index": "较冷", /*穿衣指数*/
            "dressing_advice": "建议着大衣、呢外套加毛衣、卫衣等服装。",   /*穿衣建议*/
            "uv_index": "中等",   /*紫外线强度*/
            "comfort_index": "",/*舒适度指数*/
            "wash_index": "较适宜",    /*洗车指数*/
            "travel_index": "适宜",   /*旅游指数*/
            "exercise_index": "较适宜",    /*晨练指数*/
            "drying_index": ""/*干燥指数*/
        },
        "future": [ /*未来几天天气*/
            {
                "temperature": "28℃~36℃",
                "weather": "晴转多云",
                "weather_id": {
                    "fa": "00",
                    "fb": "01"
                },
                "wind": "南风3-4级",
                "week": "星期一",
                "date": "20140804"
            },
            {
                "temperature": "28℃~36℃",
                "weather": "晴转多云",
                "weather_id": {
                    "fa": "00",
                    "fb": "01"
                },
                "wind": "东南风3-4级",
                "week": "星期二",
                "date": "20140805"
            },
            {
                "temperature": "27℃~35℃",
                "weather": "晴转多云",
                "weather_id": {
                    "fa": "00",
                    "fb": "01"
                },
                "wind": "东南风3-4级",
                "week": "星期三",
                "date": "20140806"
            },
            {
                "temperature": "27℃~34℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "东南风3-4级",
                "week": "星期四",
                "date": "20140807"
            },
            {
                "temperature": "27℃~33℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "东北风4-5级",
                "week": "星期五",
                "date": "20140808"
            },
            {
                "temperature": "26℃~33℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "北风4-5级",
                "week": "星期六",
                "date": "20140809"
            },
            {
                "temperature": "26℃~33℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "北风4-5级",
                "week": "星期日",
                "date": "20140810"
            }
        ]
    },
    "error_code": 0
}

然后,在项目新建一个Weather.cs的类,然后进入这个文件,在菜单栏里选择编辑——>选择性粘贴——>将Json粘贴为类,如下图:

之后,Visual Studio在Weather.cs文件中将自动生成下面代码:

// Weather.cs
namespace ConsoleForNewtonsoft
{
    class Weather
    {

        public class Rootobject
        {
            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 Future[] 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 city { get; set; }
            public string date_y { get; set; }
            public string week { get; set; }
            public string temperature { get; set; }
            public string weather { get; set; }
            public Weather_Id weather_id { get; set; }
            public string wind { 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_Id
        {
            public string fa { get; set; }
            public string fb { get; set; }
        }

        public class Future
        {
            public string temperature { get; set; }
            public string weather { get; set; }
            public Weather_Id1 weather_id { get; set; }
            public string wind { get; set; }
            public string week { get; set; }
            public string date { get; set; }
        }

        public class Weather_Id1
        {
            public string fa { get; set; }
            public string fb { get; set; }
        }

    }
}

添加Newtonsoft.Json

  • 解决方案资源管理器 —> 引用右击—>管理NuGet程序包搜索Newtonsoft.Json并安装

将Json字符串转化为Json对象的函数

 void ToJsonObject(string str)
 {
     // 下面两句话就可以将Json字符串解析为Json对象,是不是很简单(乛◡乛)
     JsonSerializer json = JsonSerializer.Create();
     Rootobject weather = json.Deserialize<Rootobject>(new JsonTextReader(new StringReader(str)));

     // 随便选几个值测试一下对象是不是创建成功了,相信我,成功了(乛◡乛)
     Console.WriteLine("resultcode: " + weather.resultcode);
     Console.WriteLine("reason:" + weather.reason);
     Console.WriteLine("result.today.temperature:" + weather.result.today.temperature);
 }
  • 在控制台打印出来的结果:

将Json对象转化为Json字符串的函数

// 将Json对象解析为Json字符串
private static void ToJsonString()
{
    // 打开文件,下面替换为你要保存的文件路径
    FileStream file = new FileStream("C:\\Users\\HP\\Desktop\\output.txt", FileMode.OpenOrCreate);
    TextWriter stream = new StreamWriter(file);

    // 下面两句话就可以将Json对象解析为Json字符串,是不是也很简单(乛◡乛)
    var json = JsonSerializer.Create();
    json.Serialize(stream, weather);

    // 关闭文件流很重要,之前没有关闭流文件导致后面的数据莫名其妙地丢失了
    stream.Close();
}

打开文件,你将看到转化完的Json字符串

FYI

项目下载:ConsoleForNewtonsoft

这个项目是运行在控制台上的C# 程序,同样适用于UWP程序。

顺便推荐一个方便的在谷歌浏览器上查看Json字符串(其实还有很多其他强大的功能)的插件:WEB前端助手(FeHelper)

显示效果截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值