zoukankan      html  css  js  c++  java
  • WP8解析JSON格式(使用Newtonsoft.Json包)

    DOTA2 WebAPI请求返回的格式有两种,一种是XML,一种是JSON,默认是返回JSON格式。

    这里举一个简单的解析JSON格式的例子(更多JSON操作):

    {
        "response": {
            "players": [
                {
                    "steamid": "76561198092319753",
                    "communityvisibilitystate": 1,
                    "profilestate": 1,
                    "personaname": "偶买噶、Scohura",
                    "lastlogoff": 1396240726,
                    "profileurl": "http://steamcommunity.com/profiles/76561198092319753/",
                    "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/f9/f90a468b223389164861722c599318216b388f18.jpg",
                    "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/f9/f90a468b223389164861722c599318216b388f18_medium.jpg",
                    "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/f9/f90a468b223389164861722c599318216b388f18_full.jpg",
                    "personastate": 0
                }
            ]
            
        }
    }

    解析代码如下,输入Stream流转为String就是上面的文本

            private void praseJSON(Stream json)
            {
                JObject user =JObject.Parse(new StreamReader(json).ReadToEnd());
                JObject userdata = (JObject)((JArray)(user["response"]["players"]))[0];
                //昵称赋值、溢出部分使用省略号代替
                username.Text = userdata["personaname"].ToString();
                username.TextTrimming = TextTrimming.WordEllipsis;
                username.FontSize = (this.Height - 80) / 3;
                //状态赋值
                switch (userdata["personastate"].ToString())
                {
                    case "0":
                        if (userdata["communityvisibilitystate"].ToString().Equals("1"))
                        {
                            statusText = "该用户资料未公开";
                        }
                        else
                        {
                            statusText = "离线";
                        }
                        break;
                    case "1":
                        statusText = "在线";
                        break;
                    case "2":
                        statusText = "忙碌";
                        break;
                    case "3":
                        statusText = "离开";
                        break;
                    case "4":
                        statusText = "打盹";
                        break;
                    case "5":
                        statusText = "想交易";
                        break;
                    case "6":
                        statusText = "想游戏";
                        break;
                    default: break;
                }
                status.Text = statusText;
                status.FontSize = (this.Height - 80) / 3;
                //状态辅助赋值
                if (!userdata["personastate"].ToString().Equals("0"))
                {
                    try
                    {
                        extraText = userdata["gameextrainfo"].ToString() + "  游戏中";
                        username.Foreground = new SolidColorBrush(Colors.Green);
                        status.Foreground = new SolidColorBrush(Colors.Green);
                        extra.Foreground = new SolidColorBrush(Colors.Green);
                    }
                    catch
                    {
                        username.Foreground = new SolidColorBrush(Colors.Blue);
                        status.Foreground = new SolidColorBrush(Colors.Blue);
                        extra.Foreground = new SolidColorBrush(Colors.Blue);
                    }
                }
                else
                {
                    extraText = "上次在线时间:" + Static.UtoD(userdata["lastlogoff"].ToString());
                    username.Foreground = new SolidColorBrush(Colors.Gray);
                    status.Foreground = new SolidColorBrush(Colors.Gray);
                    extra.Foreground = new SolidColorBrush(Colors.Gray);
                }
                extra.Text = extraText;
                //头像赋值
                BitmapImage bitImg = new BitmapImage(new Uri(userdata["avatarfull"].ToString()));
                head.Source = bitImg;
            }

    说明:

    JSON格式的优势在于,通过JObject["Name"] JArray[Index]就能获得所需的数据,所占的体积小。

  • 相关阅读:
    IDEA怎么自动生成serialVersionUID
    使用gcc的-l参数的时候,怎么查找函数所在库的位置
    有一个10 G 的文件,你只有有一个 2 G 的内存,找出重复最多的数字
    gdb调试使用教程
    使用autoscan自动生成makefile文件
    如何查看yum命令安装的软件的安装目录
    手机QQ邮箱app有未读邮件,图标右上角没有红色小圆点的解决方案
    谷歌google帐号(邮箱)注册时,提示此电话号码无法用于验证
    Notepad++编写的shell脚本在linux下无法执行的解决方法
    linux如何配置普通用户(非root用户)的sudo权限
  • 原文地址:https://www.cnblogs.com/Scohura/p/3636904.html
Copyright © 2011-2022 走看看