2014-10-08 5 views
0

자바에서 Json 데이터를 가져 오는 중이며 Json은 구조가 다르지만 항상 같지는 않습니다. 예 :데이터를 가져올 때 Json 배열을 피하는 방법

{"0":{"id"="255",name="example"},"1":{"news_id":"47221","news_infos":{"title":"test","date":"2014-05-14 17:44:02","shared":"47"},"website":"test.it"},"3":{"id"="55885",name="foo"}} 

이것은 단지 예입니다. 제가 알고 싶은 것은 두 번째 것을 건너 뛸 수있는 방법입니다. 두 번째 것은 JsonArray라고 가정합니다. 이것은 자바에서 내가하는 일의 예입니다.

for (int i = 0; i < jObj.length() ; i++) { 
       try { 
        JSONObject obj = jObj.getJSONObject(i); //Suppose that every entry in the Json is an object and not a JsonArray. 
        if (!obj.isNull("titrenews")) { 
         Home home = new Home(); 
         Log.i("Infos","Yes"); 
         home.setNomC(obj.getString("titrenews")); 
         home.setPhotoAr(photoNews); 
         home.setText(obj.getString("textnews")); 
         home.setNbrSick(obj.getString("sicks")); 

         homeList.add(home); 

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

요약하면 json 데이터가 있습니다. JsonObject와 JsonArray로 구성되어 있습니다. 원하는 것은 jsonArray 항목을 건너 뛰고 피하는 것입니다.

그래서 어떤 해결책을주십시오! 당신에게

for (Object obj : jObj) { 
    if (obj instanceof JSONObject){ 
     //do something 
    } 
    else if (obj instanceof JSONArray){ 
     continue;//skip 
    } 
} 
+0

JSON 라이브러리/파서 사용 – salyh

+0

org.json java from – Karimx

+0

json 예제가 유효하지 않은 json (http://jsonformatter.curiousconcept.com에서 확인)이고 json 배열이 없습니다. 완전한 예제 (완벽한 json과 완전한 소스 코드)를 제공 할 수 있습니까? – salyh

답변

0

이 방법을 시도?
+0

감사합니다! – Karimx

관련 문제