2013-12-18 3 views
0

어떻게 아래의 json을 추출하여 arraylist에 저장할 수 있습니까? Json을 Arraylist 안드로이드에 추출하고 저장하십시오.

{ 
      "trains": { 
       "train": [ 
        { 
         "@id": 1000000103, 
         "@version": 1, 
         "@status": "active", 
         "@name": "dffffff", 
         "@description": "ffffff half of the nineteenth century.", 
         "@city": "fff", 
         "@phone": "+230 595-1454", 
         "@email": "[email protected]", 
         "@website": "www4u", 
         "@latitude": -5.2882, 
         "@longitude": 3.499, 
         "@defaultLocale": "", 
         "@holes": 48, 
         "@par": 72, 
         "@slope": 0, 
         "@distance": 5.005273, 
         "circuits": { 
          "circuit": [] 
         }, 
         "localizations": { 
          "localization": [] 
         } 
        }, 
        { 
         "@id": 1000000105, 
         "@version": 1, 
         "@status": "active", 
         "@name": " xClub", 
         "@description": "", 
         "@city": " xlet", 
         "@phone": "+44465\t", 
         "@email": "", 
         "@website": "wweffl.com", 
         "@latitude": -2.040318, 
         "@longitude": 54548, 
         "@defaultLocale": "", 
         "@holes": 18, 
         "@par": 32, 
         "@slope": 0, 
         "@distance": 2441673, 
         "circuits": { 
          "circuit": [] 
         }, 
         "localizations": { 
          "localization": [] 
         } 
        } 
       ] 
      } 

    } 

try { 

       jobj_trouve_train = new JSONObject(reponse_trouve_train); 

       String jsonobj = jobj_trouve_golf.getString("trains"); 
       //String jsonobj1 = jobj_trouve_golf.getString("train"); 

       //jobj_trouve_train = new JSONObject(reponse_trouve_train); 

       //jsonArray = jobj_trouve_golf.getJSONArray("trains"); 
       //jsonArray= new JSONArray(jsonobj); 

       //System.out.println("jsonArray "+jsonArray); 

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

답변

2

잘 될 것입니다 ..

try { 

       jobj_trouve_train = new JSONObject(reponse_trouve_train); 

       JSONObject jsonobj = jobj_trouve_train.getJSONObject("trains"); 
       JSONArray jsonobj1 = jsonobj.getJSONArray("train"); 

      for(int i = 0;i< jsonobj1.length();i++){ 
       JSONObject jsonj = jsonobj1.getJSONObject(i); 

       System.out.println("@id "+jsonj.getString("@id")); 
       // Same for remaining all 
       } 

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

        e.printStackTrace(); 
     } 
+0

감사합니다. JSONArray jsonobj1 = jsonobj.getJSONArray ("train"); 해결책은 aligato이었다 – Dimitri

0

당신은 objects으로 JSON을 구문 분석 구글에서 개발 한 Gson 라이브러리를 사용할 수 있습니다 작업. https://code.google.com/p/google-gson/ 그리고, 샘플 예제는 여기에 있습니다 : : 참조는 여기에 내가 혼동하지 않은 경우 JSON Parsing with GSON

0
JSONObject trains = jobj_trouve_train.getJSONObject("trains"); 
JSONArray trainArray = trains.getJSONArray("train"); 

JSONObject train1 = trainArray.getJSONObject(0); 
... 

어쩌면이 일이 시도

0
JSONArray jasonArray = new JSONArray(result.getProperty("trains").toString()); 

for (int i = 0; i < jasonArray. 길이(); 나는 {

JSONObject jsonObj = jasonArray.optJSONObject(i); 
trainObject = new JSONObject(jsonObj.optString("train").toString()); 
int id = trainObject.optInt("id"); 

and so on.. 

}

0
public Object void toObject(String jsonMsg) throws JSONException { 
    JSONObject trains= (JSONObject) new JSONTokener(jsonMsg).nextValue(); 
    if(trains.has("trains")){ 
       JSONArray train = (JSONArray) new JSONTokener(object.getString("train")).nextValue(); 
       for (int t = 0; t < jsonArray.length(); t++) { 
        String temp = jsonArray.getJSONObject(t).toString(); 
        JSONObject object = (JSONObject) new JSONTokener(temp).nextValue(); 
        if(object.has("@id")) 
         object.getString("@id"); 
        // now similar procedure of reading 
        // read values and save it in an "object" 
    }  
    return savedObject; 
} 

) + + - 테스트, 당신의 JSON 데이터와 일치하는 코드를 수정하는 데 노력했다. 나머지를 완료하십시오.

관련 문제