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]就能获得所需的数据,所占的体积小。

转载于:https://www.cnblogs.com/Scohura/p/3636904.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值