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. ...
随机推荐
- Java基本数据类型总结
基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型.它们是我们编程中使用最频繁的类型.java是一种强类型语言,第一次申明变量必须说明数据类型,第一次变量赋值称为变量的初始化. 1. Java ...
- 教你怎么快速配置 React
导读 React 是一个构建用户界面的库,而它只是组成一个应用的一部分.应用还有其他的部分——风格.路由器.npm 模块.ES6 代码.捆绑和更多——这就是为什么使用它们的开发者不断流失的原因.这被称 ...
- Git使用- 基本命令
$ git config --global user.name "Your Name" 全局 name 设置 $ git config --global user.email ...
- css-关于文本
1. 使用 text-overflow:ellipsis; 超出部分会变成省略号 http://www.w3school.com.cn/tiy/t.asp?f=css3_text-overflow 1 ...
- Buffer类
输入流中可以通过缓冲区来加大读取的效率,sun公司感觉可以加快执行效率,他就为我们提供了一个类来操作缓存区. Buffer来头的类:所有缓冲流都是以Buffer开头的: 学习缓冲流的作用: Buffe ...
- linux I/O复用
转载自:哈维.dpkirin url:http://blog.csdn.NET/zhang_shuai_2011/article/details/7675797 http://blog.csdn.Ne ...
- css实现隐藏滚动条
demo1: html <div class="outer-container"> <div class="inner-container"& ...
- UE3名称结构(Name)
解释说明: (1) 直接通过FName的index进行比较来判断两个FName是否相等 (2) 通过FName的index从全局Names数组中取出对应的FNameEntry,可以获得FName的字符 ...
- 12.Java中Comparable接口,Readable接口和Iterable接口
1.Comparable接口 说明:可比较(可排序的) 例子:按照MyClass的y属性进行生序排序 class MyClass implements Comparable<MyClass> ...
- Js实现string.format
经常需要动态拼接html字符串,想到用类似于.net的string.format函数比较好,于是找了下,stackoverflow的代码: if (!String.prototype.format) ...