2013-08-15 5 views
-2

thumbPathscaptions을 JSON 파일에서 가져 오려고합니다.JSON이 내 객체에 액세스 할 수 없습니다.

JSON 파일 : 나는 thumbPaths에서 얻는 이미지를 처리 ​​할 수 ​​있도록 자신의 배열에 저장하려고

"pictures": [ 

     { 

      "title": "Animals", 
      "gallery": 

      [ 

       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       } 

      ] 

     }, 
     { 

      "title": "Auroras", 
      "gallery": 

      [ 

       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       } 

      ] 

     }, 
     { 

      "title": "Boats", 
      "gallery": 

      [ 

       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       }, 
       { 
        "path":"", 
        "thumbPath":"", 
        "caption":"" 
       } 

      ] 

     } 

임. 은 내가 말하려고 위의 AsyncTask를

public class getJSONObjects extends AsyncTask<String, Void, String[]>{ 

     @Override 
     protected String[] doInBackground(String... URL) { 
      // Access the JSONHandler for the URL 
      Log.d(tag, "in background on getJSON Artist Gallery."); 
      //initalize parser 
      JSONParser jparse = new JSONParser(); 
      //call objects   
       JSONObject json = jparse.getJSONFromUrl(URL); 
       try { 
        pictures = new JSONArray(json.getString(TAG_PICTURES)); 
        Log.d(tag, "after pictures"); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       // looping through All Pictures 
       for(int i = 0; i < pictures.length(); i++){ 
        Log.d(tag, "in loop for pictures"); 
        Log.d(tag, "Index:" + i); 
        JSONObject c; 
        try { 
         c = pictures.getJSONObject(i); 
         String title2 = c.getString(TAG_TITLE); 
         titles[i] = title2; 
         gallery = new JSONArray(c.getString(TAG_GALLERY)); 
         Log.d(tag, "just got gallery array"); 
         Log.d(tag, "before if statement"); 
         Log.d(tag, "title:" + titles[i].toString()); 
         if(title2 == titleTextView.getText().toString()){ 
          Log.d(tag, "inside if statement"); 
          for(int z = 0; z < gallery.length(); z++){ 
           try { 
            JSONObject d = gallery.getJSONObject(z); 
            String thumbPath = d.getString(TAG_THUMBPATHS); 
            String captions = d.getString(TAG_CAPTIONS); 
            Log.d(tag, "thumbPath:" + thumbPath); 
            Log.d(tag, "captions:" + captions); 

           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 
          } 
         } 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

이를 달성하기 위해 시도하고 그 제목은 만에 thumbPaths 및 캡션을 잡아해야한다는 JSON 배열의 제목 == 경우 그 제목 아래 갤러리. 그렇게하면 나는 오직 내가 필요한 것을 잡을 뿐이다.

JSON 파일에서 적절한 정보를 얻을 수 있습니까?

+1

나는'title2.equals (titleTextView.getText(). toString())'이 아니라'title2 == ... '이 아니라고 생각합니다. – Ali

+0

@Ali 당신의 권리, 고마워요! – Keeano

+0

또한 필요한 경우 "잡아"싶으면 스트리밍 JSON API를 사용하여 조사해야한다고 생각합니다. 그렇게하면 큰 문자열을 메모리에 저장할 필요없이 객체를 만날 때 객체를 만들 수 있습니다 (일부 장치에서 충돌이 발생할 수 있음). 약간의 작업이지만 효율적인 메모리가 좋습니다. 나는 [Jackson] (http : //wiki.fasterxml.com/JacksonInFiveMinutes)에 대해 – Ali

답변

1

JSON value에 액세스하려면 해당 key을 사용하여 액세스해야합니다.

이 경우 구조는 배열과 키가 혼합 된 JSON 객체입니다. 주어진 구조에 따라 첫 번째 keypictures입니다.

첫 번째 요소에 두 개의 키 (titlegallery)가있는 유형은 array입니다. 우리는 gallery에 관심이 있습니다. gallery은 다시 배열의 인스턴스입니다 (객체의 배열입니다). 첫 번째 요소에 액세스하려면 갤러리에 [0]을 사용하여 첫 번째 객체를 가져옵니다.

이 개체는 3 개의 키 값 쌍으로 구성됩니다. 특정 값을 얻으려면 object[key]을 사용하여 해당 키를 사용하여 액세스 할 수 있습니다.

예를

를 들어

thumbPaths의 첫 번째 인스턴스에 액세스하는 데 도움이 :

+0

조금 더 설명해 주시겠습니까? 아직 Android에서 JSON 구문 분석을 사용하여 조금 새로운 기능을 제공합니다. – Keeano

+0

네 조금 아프다. – woofmeow

+0

@ woofmeow – Keeano

1

당신의 문서를 체크 아웃해야한다 등등 captions

 
Object["pictures"][0]["gallery"][0]["thumbPath"] // will give : "" 
Object["pictures"][0]["gallery"][0]["caption"] // will give : "" 

와 ..

희망합니다 json 패키지 here.

, 이전에 언급 한 바와 같이, 또한

gallery = c. getJSONArray(TAG_GALLERY); 

gallery = new JSONArray(c.getString(TAG_GALLERY)); 

같은 물건을 교체 문자열 비교에 대한 equals()하지 ==를 사용합니다.

관련 문제