2012-01-20 4 views
0

페이스 북에서 json 객체를 파싱 중입니다. 내 facebook json 객체에는 키 배열 "data"안에 문자열 키가 있습니다.안드로이드에서 json을 파싱 할 때 null 값을 건너 뛰고 있습니까?

내 코드를 다음과 같이

"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 개체를 구문 분석 할 때, 문자열 키가 "좋아"때때로 존재하고, 때로는 "데이터"jsonArray에 존재하지 않습니다. 그래서 JsonString을 구문 분석 할 때 "likes"가 나타나면 null 값을 파싱 할 때가 종종 JSONException이됩니다. "null"json 값을 건너 뛰는 방법이 있습니까?

도와주세요.

+0

간단한 ..... 넣어 시도 캐치 & 그것을 잡을인지 여부를 확인할 수 있습니다. – Lucifer

답변

1

편집 : 이것은 키를 확인하는 것입니다. 당신은 열쇠가 때때로 존재한다고 언급했기 때문에. 그 값을 뜻 했니?

사용 JSONObjects 방법은 (키)

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

이 값이 isNull에와 NULL 체크 ("이름")

+0

사례 확인 방법 ** 데이터 : [{key : value}, null, {key : value}] ** – Manish

2

잡으려고하면 다음 프로세스를 건너 뜁니다. isNull ("name") 메소드로 확인할 수 있습니다.

관련 문제