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"]))[];
//昵称赋值、溢出部分使用省略号代替
username.Text = userdata["personaname"].ToString();
username.TextTrimming = TextTrimming.WordEllipsis;
username.FontSize = (this.Height - ) / ;
//状态赋值
switch (userdata["personastate"].ToString())
{
case "":
if (userdata["communityvisibilitystate"].ToString().Equals(""))
{
statusText = "该用户资料未公开";
}
else
{
statusText = "离线";
}
break;
case "":
statusText = "在线";
break;
case "":
statusText = "忙碌";
break;
case "":
statusText = "离开";
break;
case "":
statusText = "打盹";
break;
case "":
statusText = "想交易";
break;
case "":
statusText = "想游戏";
break;
default: break;
}
status.Text = statusText;
status.FontSize = (this.Height - ) / ;
//状态辅助赋值
if (!userdata["personastate"].ToString().Equals(""))
{
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]就能获得所需的数据,所占的体积小。
WP8解析JSON格式(使用Newtonsoft.Json包)的更多相关文章
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
fastJson对于json格式字符串的解析主要用到了一下三个类: JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. JSONObject:fas ...
- Newtonsoft.Json高级用法DataContractJsonSerializer,JavaScriptSerializer 和 Json.NET即Newtonsoft.Json datatable,dataset,modle,序列化
原文地址:https://www.cnblogs.com/yanweidie/p/4605212.html Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而 ...
- [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类
[.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...
- FastJson学习:JSON格式字符串、JSON对象及JavaBean之间的相互转换
当前台需要传送一系列相似数据到后端时,可以考虑将其组装成json数组对象,然后转化为json形式的字符串传输到后台 例如: nodes = $('#PmPbsSelect_tree').tree('g ...
- $.each遍历json对象(java将对象转化为json格式以及将json解析为普通对象)
查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. var json = [ {"id":"1","tagName": ...
- WP8解析XML格式文件
DOTA2 WebAPI请求返回的格式有两种,一种是XML,一种是JSON,默认是返回JSON格式,如果要返回XML格式的话,需要在加上format=xml. 这里举一个简单的解析XML格式的例子(更 ...
- List转换成json格式字符串,json格式字符串转换成list
一.List转换成json字符串 这个比较简单,导入gson-x.x.jar, List<User> users = new ArrayList<User>(); Gson g ...
- json格式转换(json,csjon)(天气预报)
json格式数据默认为string,可以使用eval()函数或者json模块将其转换为dict.标准Json字符串必须使用双引号(")而不能使用单引号('),否则从字符串转换成dict类型会 ...
- C#将对象转换成JSON字符串,Newtonsoft.Json (JSON.NET)
官方API说明文档 http://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json.htm http://www.newtonsoft.com/ ...
- Json格式循环遍历,Json数组循环遍历
Json格式数据如何遍历,这里我们可以用for..in实现 例如最简单的json格式 , 'handsome' : 'yes' }; for( var key in json1 ){ console. ...
随机推荐
- rpcbind服务说明及关闭
rpcbind服务说明及关闭 Posted by 破冰 on -- : Tuesday rpcbind服务停止命令 service portmap stop redhat 的rpc.statd服务: ...
- shell语法
基本语法列表 #linux组成:内核+工具 #linux启动: . getty:提示登录名和密码,输入之后调用login . login:login验证用户名和密码,然后调用shell . shell ...
- Node.js配合node-http-proxy解决本地开发ajax跨域问题
情景: 前后端分离,本地前端开发调用接口会有跨域问题,一般有以下3种解决方法: 1. 后端接口打包到本地运行(缺点:每次后端更新都要去测试服下一个更新包,还要在本地搭建java运行环境,麻烦) 2. ...
- mkdir命令
[mkdir] 创建目录 mkdir ===make directory 命令格式: mkdir [OPTION]... DIRECTORY 命令功能: 通过 mkdir 命令可以实现在指 ...
- [Sass]声明变量
[Sass]声明变量 定义变量的语法: 在有些编程语言中(如,JavaScript)声明变量都是使用关键词"var"开头,但是在 Sass 不使用这个关键词,而是使用大家都喜欢的美 ...
- node09-cookie
目录:node01-创建服务器 node02-util node03-events node04-buffer node05-fs node06-path node07-http node08-exp ...
- Java_File类讲解_打印目录树状结构_递归算法
package cn.xiaocangtian.testFile; import java.io.File; public class FileTree { public static void ma ...
- Docker常用命令大全
1. 查看docker信息(version.info) # 查看docker版本 $docker version # 显示docker系统的信息 $docker info 2. 对image的操作(s ...
- ***git自动化部署总结
在网上搜了一堆没用找到太仔细的教程,于是花费2天时间,自己研究了下.现在分享如下: 思路:利用crontab定时去去执行git pull脚本 首先,用git clone命令将中央库的代码下载到阿里云 ...
- AutoMapper实现自动CreapMap
标题是个噱头,完全不写代码自动是不现实的,只是简化了CreateMap.方法也是很粗糙的,看看吧. 我想在使用AutoMapper的时候最恶心的一定是写了一个Profile,里边有n行 Mapper. ...