2016-10-15 3 views
0

이 jsonarray "이미지"를 구문 분석하는 방법 ??android에서 구문 분석 JsonArray

I이 JSON 파싱 ,,하지만, 이미지의 각 소자 어레이 내부에서, 그 소자 내부에 "화상" Jsonarray "데이터"를 얻을 수는 >>>>

이 응답 Json

이 내 코드

내가보기 엔 그것을 구문 분석 GSON를 사용하는 것이 좋습니다
   JSONObject responseObject = new JSONObject(response); 

       JSONArray newsJsonArray = responseObject.getJSONArray("data"); 
       final List <AndroidVersion> newsList = new ArrayList <AndroidVersion>(); 

       newsImages = new ArrayList<String>(); 
       newsids = new ArrayList<String>(); 
       newsnames = new ArrayList<String>(); 
       newshotelsid = new ArrayList<String>(); 
       newscontents = new ArrayList<String>(); 
       newstimesto = new ArrayList<String>(); 
       newscost = new ArrayList<String>(); 
       newstimesfrom = new ArrayList<String>(); 
       newscountry = new ArrayList<String>(); 
       newscity = new ArrayList<String>(); 
       newstype = new ArrayList<String>(); 
       newsImages = new ArrayList<String>(); 

        for (int j = 0; j < newsJsonArray.length(); j++) { 
         AndroidVersion news = new AndroidVersion(); 
         if (newsJsonArray.getJSONObject(j).has("id")) { 
          newsids.add(newsJsonArray.getJSONObject(j).getString("id")); 
         } 
         if (newsJsonArray.getJSONObject(j).has("name")) { 
          news.setName(newsJsonArray.getJSONObject(j).getString("name")); 
          newsnames.add(newsJsonArray.getJSONObject(j).getString("name")); 

         } 
         if (newsJsonArray.getJSONObject(j).has("desc")) { 
          news.setdesc(newsJsonArray.getJSONObject(j).getString("desc") 
            .replaceAll("<p>", "").replaceAll("<\\/p>\\r\\n", "").replaceAll("&nbsp;", "")); 
          newscontents.add(newsJsonArray.getJSONObject(j).getString("describtion")); 
         } 

         if (newsJsonArray.getJSONObject(j).has("country")) { 
          news.setcountry(newsJsonArray.getJSONObject(j).getString("country")); 
          newscountry.add(newsJsonArray.getJSONObject(j).getString("country")); 
         } 

         if (newsJsonArray.getJSONObject(j).has("city")) { 
          news.setcity(newsJsonArray.getJSONObject(j).getString("city")); 
          newscity.add(newsJsonArray.getJSONObject(j).getString("city")); 
         } 

         if (newsJsonArray.getJSONObject(j).has("date_from")) { 
          news.setdate_from(newsJsonArray.getJSONObject(j).getString("date_from")); 
          newstimesfrom.add(newsJsonArray.getJSONObject(j).getString("date_from")); 
         } 
         if (newsJsonArray.getJSONObject(j).has("date_to")) { 
          news.setdate_to(newsJsonArray.getJSONObject(j).getString("date_to")); 
          newstimesto.add(newsJsonArray.getJSONObject(j).getString("date_to")); 
         } 
         if (newsJsonArray.getJSONObject(j).has("num persons")) { 
          news.sethotel_id(newsJsonArray.getJSONObject(j).getString("num persons")); 
          newshotelsid.add(newsJsonArray.getJSONObject(j).getString("num persons")); 
         } 
         if (newsJsonArray.getJSONObject(j).has("price")) { 
          news.setprice(newsJsonArray.getJSONObject(j).getString("price")); 
          newscost.add(newsJsonArray.getJSONObject(j).getString("price")); 
         } 
         if (newsJsonArray.getJSONObject(j).has("images")) { 
          news.setImage(newsJsonArray.getJSONObject(j).getString("images")); 
          newsImages.add(newsJsonArray.getJSONObject(j).getString("images")); 
         } 
         newsList.add(news); 
        } 
+2

가능한 복제 [에서 JSON을 구문 분석하는 방법 안드로이드] (http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) –

+0

당신은 이미지의 URL을 받았습니까 목록에 es ?? – Manish

+2

이미지는 문자열이 아니라 배열입니다. 또 다른 루프가 필요하다. –

답변

0

입니다.

json 응답과 일치하는 구조로 응답 개체를 만듭니다. 그런 다음 하나의 객체를 부품으로 생성 할 필요없이 사용할 수 있습니다.

0

변경이 부분 :

if (newsJsonArray.getJSONObject(j).has("images")) { 
    news.setImage(newsJsonArray.getJSONObject(j).getString("images")); 
    newsImages.add(newsJsonArray.getJSONObject(j).getString("images")); 
} 

에 :

if(newsJsonArray.getJSONObject(j).getJSONArray("images") != null) { 
    JSONArray jArray = newsJsonArray.getJSONObject(j).getJSONArray("images"); 
    for (int k=0; k<jArray.length(); k++) { 
     news.setImage(jArray.getString(k)); 
     newsImages.add(jArray.getString(k)); 
    } 
} 
+0

getJsonArray에 오류가 있습니다 –

+0

죄송합니다, 그것은 오타 였고 getJsonArray 대신 getJSONArray 여야합니다. 나는 업데이트했다. –

0

는 아래처럼 JSONArray에서 직접 문자열 객체를 사용할 수 있습니다

JSONArray array = object.getJSONArray("images"); 

for (int i = 0; i < array.length(); i++) { 

String image = array.getString(i); 

} 
관련 문제