在android中解析json时跳过空值?

时间:2012-01-20 02:40:47

标签: android facebook json

我正在从facebook解析json对象。 在我的facebook json对象中,键数组“data”中有字符串键。

我的代码如下,

"data": [
  {
     "id": "100001211447563_300696056647440",
     "from": {
        "name": "Seho Lee",
        "id": "100001211447563"
     },
     "story": "Seho Lee is now using Facebook in English (US).",
     "story_tags": {
        "0": [
           {
              "id": 100001211447563,
              "name": "Seho Lee",
              "offset": 0,
              "length": 8
           }
        ]
     },
     "type": "status",
     "created_time": "2012-01-19T09:13:04+0000",
     "updated_time": "2012-01-19T09:13:04+0000",
     "comments": {
        "count": 0
     }
  },
  {
     "id": "100001211447563_298802933503419",
     "from": {
        "name": "Seho Lee",
        "id": "100001211447563"
     },
     "story": "Seho Lee started using Graph API Explorer.",
     "story_tags": {
        "23": [
           {
              "id": 145634995501895,
              "name": "Graph API Explorer",
              "offset": 23,
              "length": 18
           }
        ],
        "0": [
           {
              "id": 100001211447563,
              "name": "Seho Lee",
              "offset": 0,
              "length": 8
           }
        ]
     },
     "picture": "http://photos-a.ak.fbcdn.net/photos-ak-snc1/v43/11/145634995501895/app_1_145634995501895_4870.gif",
     "link": "http://developers.facebook.com/tools/explorer/",
     "caption": "A tool to help you browse objects within the Facebook Graph API, manage permissions, obtain access tokens and generally learn how it all works.",
     "actions": [
        {
           "name": "Comment",
           "link": "http://www.facebook.com/100001211447563/posts/298802933503419"
        },
        {
           "name": "Like",
           "link": "http://www.facebook.com/100001211447563/posts/298802933503419"
        }
     ],
     "type": "link",
     "created_time": "2012-01-16T09:47:57+0000",
     "updated_time": "2012-01-16T09:47:57+0000",
     "comments": {
        "count": 0
     }
  },
  {
     "id": "100001211447563_298789650171414",
     "from": {
        "name": "Seho Lee",
        "id": "100001211447563"
     },
     "story": "Seho Lee likes myFBCovers.com.",
     "story_tags": {
        "15": [
           {
              "id": 160901873998019,
              "name": "myFBCovers.com",
              "offset": 15,
              "length": 14
           }
        ],
        "0": [
           {
              "id": 100001211447563,
              "name": "Seho Lee",
              "offset": 0,
              "length": 8
           }
        ]
     },
     "type": "status",
     "created_time": "2012-01-16T08:57:33+0000",
     "updated_time": "2012-01-16T08:57:33+0000",
     "comments": {
        "count": 0
     }
  }

当我解析这个json对象时,String键“喜欢”有时存在,有时在“data”jsonArray中不存在。因此,当我解析JsonString“喜欢”时,有时我得到JSONException因为我正在解析空值。有没有办法跳过“null”json值?

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

如果尝试捕获它,将跳过下一个进程。 你可以通过isNull(“name”)方法检查。

答案 1 :(得分:1)

编辑:这是检查密钥。因为你有时提到密钥存在。你的意思是价值吗?

使用JSONObjects方法有(键)

data = (JSONObject) new JSONTokener(jsonObjRecv.toString()).nextValue();
if data.has("like"){
...
}

使用isNull(“name”)

检查值是否为null