您的位置:首页 > 移动开发 > Android开发

Android新浪微博开发(3)JSon解析

2012-10-11 10:47 441 查看
  我在今天之前,可以说是没有接触过JSon的解析,今天就边学边总结,同时针对新浪微博返回的JSon进行解析,帮助自己巩固吧。我学习JSon解析是参考这个网址上的内容:http://lib.open-open.com/view/open1326376799874.html。一些JSon解析的基础知识和代码,大家可以看这篇文章。这边文章里面说,android2.3提供的JSon解析类,我使用的是android2.2,依然是提供JSon解析类的。

  现在开始针对新浪微博返回的数据来进行JSon解析,现在有这里有一个我发表一个微博成功之后返回的JSon数据。

{
"created_at": "Wed Oct 10 14:20:07 +0800 2012",
"id": 3499586935431840,
"mid": "3499586935431840",
"idstr": "3499586935431840",
"text": "专注测试2天多",
"source": "<a href=\"http://open.t.sina.com.cn\" rel=\"nofollow\">未通过审核应用</a>",
"favorited": false,
"truncated": false,
"in_reply_to_status_id": "",
"in_reply_to_user_id": "",
"in_reply_to_screen_name": "",
"geo": null,
"user": {
"id": 2494519890,
"idstr": "2494519890",
"screen_name": "各种路各种盲",
"name": "各种路各种盲",
"province": "34",
"city": "16",
"location": "安徽 亳州",
"description": "",
"url": "",
"profile_image_url": "http://tp3.sinaimg.cn/2494519890/50/5614603461/1",
"profile_url": "u/2494519890",
"domain": "",
"weihao": "",
"gender": "m",
"followers_count": 1,
"friends_count": 0,
"statuses_count": 9,
"favourites_count": 0,
"created_at": "Wed Oct 26 11:01:04 +0800 2011",
"following": false,
"allow_all_act_msg": false,
"geo_enabled": true,
"verified": false,
"verified_type": -1,
"allow_all_comment": true,
"avatar_large": "http://tp3.sinaimg.cn/2494519890/180/5614603461/1",
"verified_reason": "",
"follow_me": false,
"online_status": 0,
"bi_followers_count": 0,
"lang": "zh-cn"
},
"reposts_count": 0,
"comments_count": 0,
"attitudes_count": 0,
"mlevel": 0,
"visible": {
"type": 0,
"list_id": 0
}
}


  先来一个迭代解析整个JSon数据的样例,参数json是上面新浪微博返回的JSon数据。

try
{
JSONObject jsonObject = new JSONObject(json);
for (Iterator<?> iterator = jsonObject.keys(); iterator.hasNext();)
{
String key = (String) iterator.next();
Object value = jsonObject.opt(key);
if (value instanceof JSONObject)
{
JSONObject new_value = (JSONObject) value;
for (Iterator<?> iterator2 =  new_value.keys(); iterator2.hasNext();)
{
String key2 = (String) iterator2.next();
Object value2 = new_value.opt(key2);
System.out.println(key2 + "=" + value2);
}
}
else
{
System.out.println(key + "=" + jsonObject.opt(key));
}

}
}
catch (JSONException e)
{
e.printStackTrace();
}


  因为iterator是与原来的JSon数据中元素的顺序是不同的,所以看到的输出也是不同的。

  新浪微博返回的JSon数据不可能说每一个元素对我们都是有用的,我们可以只是对某几个元素感兴趣,所以我们可以把我们感兴趣的那些元素提取出来。

  

try
{
JSONObject jsonObject = new JSONObject(json);
/* 从JSon中从指定的元素中获取整型 */
String textString = jsonObject.getString("text");
String usernameString = jsonObject.getJSONObject("user").getString("name");
String timeString = jsonObject.getString("created_at");

/* 从JSon中从指定的元素中获取整型 */
int reposts_count = jsonObject.getInt("reposts_count");
int comments_count = jsonObject.optInt("comments_count");

/* 从JSon中从指定的元素中获取boolean类型 */
boolean favorited = jsonObject.getBoolean("favorited");

/* 通过字符匹配构造适合中国人的时间字符串 */
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Date date = sdf.parse(timeString);
sdf.applyPattern("yyyy年MM月dd日hh时mm分ss秒");
timeString = sdf.format(date);

StringBuffer sb = new StringBuffer();
sb.append(usernameString).append("在").append(timeString).append("发布了一条微博,内容是:").append(textString)
.append("。目前转发数为").append(reposts_count).append(",评论数为").append(comments_count);

if (favorited)
{
sb.append("。已经收藏。");
}
else
{
sb.append("。尚未收藏。");
}

Log.i("", sb.toString());
}
catch (Exception e)
{
e.printStackTrace();
}


  获取指定的元素的方法有两个,一个是get方法,一个是opt方法。二者的区别是:

get可以将要获取的键的值转换为指定的类型,如果无法转换或没有值则抛出JSONException

opt也是将要获取的键的值转换为指定的类型,无法转换或没有值时返回用户提供或这默认提供的值

  个人认为,使用opt方法会好一点,因为不会因为取值问题产生转化之类的异常。

  有时,新浪微博也会返回给我们一些JSon数组,例如我搜索关键词"阿信",新浪微博会返回给我下面的JSon数据。

[
{
"screen_name": "阿信",
"followers_count": 11091994,
"uid": 1265020392
},
{
"screen_name": "阿信星闻",
"followers_count": 22427,
"uid": 1822541757
},
{
"screen_name": "阿信ken",
"followers_count": 10799,
"uid": 1870714545
},
{
"screen_name": "阿信歌友会",
"followers_count": 10493,
"uid": 1809449650
},
{
"screen_name": "阿信形象设计",
"followers_count": 10038,
"uid": 2122509095
},
{
"screen_name": "阿訫",
"followers_count": 515,
"uid": 1747978781
},
{
"screen_name": "阿信语录",
"followers_count": 4123,
"uid": 2091945521
},
{
"screen_name": "阿信-xiaoxiao",
"followers_count": 3761,
"uid": 2049489032
},
{
"screen_name": "阿信家的洗衣机",
"followers_count": 3526,
"uid": 1816621915
},
{
"screen_name": "阿信家的小胖话筒",
"followers_count": 2774,
"uid": 2728456684
}
]


  解析代码:

try
{
JSONArray jsonArray = new JSONArray(json);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
{
String screen_name = jsonObject.optString("screen_name");
long followers_count = jsonObject.optLong("followers_count");
String uid = jsonObject.optString("uid");
sb.append("用户名:").append(screen_name).append("\n粉丝数:").append(followers_count).append("\nuid:")
.append(uid).append("\n");
if (i != jsonArray.length() - 1)
{
sb.append("--------------------------传说中的分隔线----------------------------\n");
}
}
}

Log.i("", sb.toString());
}
catch (Exception e)
{
e.printStackTrace();
}


  另外,还有一些JSon中带有JSon类型的元素,即某一个元素的类型是一个JSon数据或者是一个JSon数据。这种情况只要获取到这个元素,继续进行JSon数据的解析就能继续解析了。

  至此,关于JSon的解析已经把经常使用到的方法都讲到了,关于JSon数据的构造由于在新浪微博中客户端并没有用到,所以就不在讲了。

  经过这3篇文章,我们就已经完全能够进行与新浪微博之间的交互,各个有权限的接口都可以使用,我们自己也能够实现自己的新浪微博客户端了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: