新浪微博的JSON解析

来源:互联网 发布:知乎名字来源 编辑:程序博客网 时间:2024/05/16 05:37

前段时间做了新浪微博的一些基本功能,因为工作上的事情获取数据后的JSON解析一直没空做,今天空下来,研究了一下,现在把结果写出来

在做这个之前我对JSON解析了解的不是很多,只能对一些简单的数据解析,对于稍微复杂一点的结构就一筹莫展了,在网上找了很多资料也没能解决

后来看了这个帖子,终于稍微摸到了一点门道:http://hi.baidu.com/iiloveloveyouyou/blog/item/cec41b1d5ccdf48087d6b68e.html/cmtid/465fd0f12cea96cc7931aa9a


首先先看一下新浪微博目前的JSON的结构

{    "statuses": [        {  //位置1            "created_at": "Tue May 31 17:46:55 +0800 2011",  // 位置2            "id": 11488058246,            "text": "求关注。",            "source": "<a href="http://weibo.com" rel="nofollow">新浪微博</a>",            "favorited": false,            "truncated": false,            "in_reply_to_status_id": "",            "in_reply_to_user_id": "",            "in_reply_to_screen_name": "",            "geo": null,            "mid": "5612814510546515491",            "reposts_count": 8,            "comments_count": 9,            "annotations": [],            "user": {  //位置3                "id": 1404376560,                "screen_name": "zaku",                "name": "zaku",                "province": "11",                "city": "5",                "location": "北京 朝阳区",                "description": "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。",                "url": "http://blog.sina.com.cn/zaku",                "profile_image_url": "http://tp1.sinaimg.cn/1404376560/50/0/1",                "domain": "zaku",                "gender": "m",                "followers_count": 1204,                "friends_count": 447,                "statuses_count": 2908,                "favourites_count": 0,                "created_at": "Fri Aug 28 00:00:00 +0800 2009",                "following": false,                "allow_all_act_msg": false,                "remark": "",                "geo_enabled": true,                "verified": false,                "allow_all_comment": true,                "avatar_large": "http://tp1.sinaimg.cn/1404376560/180/0/1",                "verified_reason": "",                "follow_me": false,                "online_status": 0,                "bi_followers_count": 215            }        },        ...    ],    "previous_cursor": 0,  // 位置4    "next_cursor": 11488013766,    "total_number": 81655}

将上面的数据简化一下就是下面的结构,K代表key,V代表value

{K : [   {  // 位置1K : V ,  // 位置2K : { K : V }  // 位置3},{  // 位置1K : V ,  // 位置2K : { K : V }  // 位置3},......],K : V    // 位置4}

好了,现在我们开始一点一点的去解析它

首先最外面的一层大括号{ ..... },这个应该使用JSONObject()去获取对象

JSONObject jsonObject = new JSONObject(json); 

位置1的数据需要一个getJSONArray()方法去获取,因为他是一个数组,[ ]之间的每一个{ ..... }代表数组的一个元素

JSONArray statusesArr = jsonObject.getJSONArray("statuses");

此时位置1的元素需要将其转化为JsonObject类

此时有2种办法可以转化

第一种:

JSONObject statusesObj = statusesArr.getJSONObject(0);  // 这里的0代表的就是第一个{},以此类推

第二种:

String statusesStr = statusesArr.getString(0);JSONObject statusesObj = new JSONObject(statusesStr);

这个时候我们就可以获取位置2的数据了

statusesObj.getString("created_at");

位置3的数据又有点比较搞了,直接贴代码

String user = statusesObj.getString("user"); // 获取位置3的值JSONObject userObj = new JSONObject(user); // 将其转化为JSONObjectString name = userObj.getString("name"); // 使用get方法获取数据

位置4的数据获取很简单,使用普通的get方法就可以获得

jsonObject.getInt("total_number");

至此,JSON数据分析完毕,大家领会这个解析的方法,基本上就可以解析新浪微博的JSON的方法了










 

原创粉丝点击