2013-06-27 1 views
1

어떻게이 Json 구조를 조사하고 각 요소 ID 이름 링크를 저장하여 목록에 입력 할 수 있습니까? 여기 내 json 파일입니다.Json Parser가 크기 = 0을 반환합니다. Android

{ 
     "data": [ 
      { 
       "id": "1214294", 
       "name": "The Pop Rock Boys", 
       "link": "http://www.deezer.com/artist/1214294", 
       "picture": "https://api.deezer.com/2.0/artist/1214294/image", 
       "nb_album": 7, 
       "nb_fan": 3, 
       "radio": false, 
       "type": "artist" 
      }, 
      { 
       "id": "813196", 
       "name": "Ringtone Pop Rock", 
       "link": "http://www.deezer.com/artist/813196", 
       "picture": "https://api.deezer.com/2.0/artist/813196/image", 
       "nb_album": 0, 
       "nb_fan": 4, 
       "radio": false, 
       "type": "artist" 
      }, 
      { 
       "id": "4165034", 
       "name": "Rock of Pop", 
       "link": "http://www.deezer.com/artist/4165034", 
       "picture": "https://api.deezer.com/2.0/artist/4165034/image", 
       "nb_album": 1, 
       "nb_fan": 0, 
       "radio": false, 
       "type": "artist" 
      }, 
      { 
       "id": "4022223", 
       "name": "instrumental/Pop/Rock", 
       "link": "http://www.deezer.com/artist/4022223", 
       "picture": "https://api.deezer.com/2.0/artist/4022223/image", 
       "nb_album": 0, 
       "nb_fan": 1, 
       "radio": false, 
       "type": "artist" 
      } 
     ], 
     "total": 4 
    } 

답변

6

초기 문자열을 배열로 파싱하고 있습니다. 그것은 배열이 아니며, "data"라는 이름의 단일 배열을 포함하는 객체입니다. 이를 JSONObject로 구문 분석 한 다음 데이터 배열이라는 이름의 배열을 가져온 다음 루프를 반복해야합니다.

try { 
     JSONArray articlesArray = new JSONObject(jString).getJSONArray("data"); 
     JSONObject aObject; 
     for(int i=0; i<articlesArray.length(); i++){ 
      aObject=articlesArray.getJSONObject(i); 

      SearchTrack a=new SearchTrack(); 
      a.setId(aObject.getString("id")); 
      a.setLink(aObject.getString("link")); 
      a.setName(aObject.getString("name")); 
      a.setPicture(aObject.getString("picture")); 
      a.setNbAlbum(aObject.getString("nb_album")); 
      a.setNbFan(aObject.getString("nb_fan")); 
      a.setRadio(aObject.getString("radio")); 
      a.setType(aObject.getString("type")); 

      articles.add(a); 
     } 
    }catch (JSONException e) { 
     e.printStackTrace(); 
    } 
관련 문제