0

I am parsing json object from facebook. In my facebook json object, there are string keys inside key array "data".

My Code as follows,

"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
     }
  }

when I parsing this json object, String key "likes" is sometimes exist and sometimes do not exist in "data" jsonArray. So when I parsing JsonString "likes", sometimes I gets JSONException since I am parsing null value. Is there any way to skipping "null" json value?

Please help me regarding this.

1
  • simple..... put try catch & catch it. Commented Jan 20, 2012 at 2:43

2 Answers 2

2

if try catch it, next processes will be skipped. you can check by isNull("name") method.

Sign up to request clarification or add additional context in comments.

Comments

1

Edit: this is to check key. since you mentioned key exists sometimes. did you mean the value?

use JSONObjects method has(key)

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

to check whether the value is null check with isNull("name")

1 Comment

how to check in dis case data:[{key:value},null,{key:value}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.