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包)的更多相关文章

  1. FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换

    fastJson对于json格式字符串的解析主要用到了一下三个类: JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. JSONObject:fas ...

  2. Newtonsoft.Json高级用法DataContractJsonSerializer,JavaScriptSerializer 和 Json.NET即Newtonsoft.Json datatable,dataset,modle,序列化

    原文地址:https://www.cnblogs.com/yanweidie/p/4605212.html Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而 ...

  3. [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类

    [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...

  4. FastJson学习:JSON格式字符串、JSON对象及JavaBean之间的相互转换

    当前台需要传送一系列相似数据到后端时,可以考虑将其组装成json数组对象,然后转化为json形式的字符串传输到后台 例如: nodes = $('#PmPbsSelect_tree').tree('g ...

  5. $.each遍历json对象(java将对象转化为json格式以及将json解析为普通对象)

    查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. var json = [ {"id":"1","tagName": ...

  6. WP8解析XML格式文件

    DOTA2 WebAPI请求返回的格式有两种,一种是XML,一种是JSON,默认是返回JSON格式,如果要返回XML格式的话,需要在加上format=xml. 这里举一个简单的解析XML格式的例子(更 ...

  7. List转换成json格式字符串,json格式字符串转换成list

    一.List转换成json字符串 这个比较简单,导入gson-x.x.jar, List<User> users = new ArrayList<User>(); Gson g ...

  8. json格式转换(json,csjon)(天气预报)

    json格式数据默认为string,可以使用eval()函数或者json模块将其转换为dict.标准Json字符串必须使用双引号(")而不能使用单引号('),否则从字符串转换成dict类型会 ...

  9. C#将对象转换成JSON字符串,Newtonsoft.Json (JSON.NET)

    官方API说明文档 http://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json.htm http://www.newtonsoft.com/ ...

  10. Json格式循环遍历,Json数组循环遍历

    Json格式数据如何遍历,这里我们可以用for..in实现 例如最简单的json格式 , 'handsome' : 'yes' }; for( var key in json1 ){ console. ...

随机推荐

  1. file_get_contents带bom

    $dmText = file_get_contents( AROOT .'data' . DS . 'DMType.json.php'); if(preg_match('/^\xEF\xBB\xBF/ ...

  2. 【Android自学日记】两种适配器的使用

    ArrayAdapter适配器: (1)用于显示基本的文字内容 (2)基本使用过程:新建适配器---创建或加载数据源---适配器加载数据源---视图加载适配器 ArrayAdapter(上下文,当前L ...

  3. 【CQOI2011】动态逆序对 BZOJ3295

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

  4. NetBeans建立跳过测试构建的快捷方式

    在项目浏览器中右键项目->属性,如图进行设置: 此后按下图即可运行自定义行为:

  5. linux无法挂载u盘

    一般插入u盘都会自动挂载,但有时挂载不了,错误提示:can't find /dev/sdb in /etc/fstab:这时可能是U盘坏了,我们当然不希望是这样.也有可能是U盘使用的接口不对应导致系统 ...

  6. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

  7. OC与Swift单例

    OC: +(instancetype)shareNetworkTools{ static id instance; static dispatch_once_t onceToken; //onceTo ...

  8. STL heap usage

    简介 heap有查找时间复杂度O(1),查找.插入.删除时间复杂度为O(logN)的特性,STL中heap相关的操作如下: make_heap() push_heap() pop_heap() sor ...

  9. 【纯css】左图右文列表,左图外框宽度占一定百分比的正方形,右上下固定,右中自动响应高度。支持不规则图片。

    查看演示 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  10. php性能剖析的几款软件

    1. xhprof (http://pecl.php.net/package/xhprof)   facebook  2009年开源 2. xdebug 3. valgrind 4. cachegri ...