2011-07-26 36 views
0

在我的腳本的這一部分仍在工作來解析我的JSON jsonArray = new JSONArray(jsonData);,然後將其添加到一個數組列表「myJSONArray」增加JSON數據的ArrayList

我不知道如何在for循環將努力從添加我的元素jsonArray到myJSONArray(arrayList)

如果我不清楚,請讓我知道,如果有任何信息我想念只是問。提前致謝。

JSON數據:

{"id":["1","2","3"],"name":["Dragon","Butterfly","Tattoo"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]} 

類IMAGE_DATA:

public class image_data { 


    public int id; 
    public String name; 
    public String thumb; 
    public String path; 


} 

ShowThumb類:

public class showThumb extends Activity{ 



    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gridlayout); 

     Bundle bundle = getIntent().getExtras(); 
     String jsonData = bundle.getString("jsonData"); 


     JSONArray jsonArray = null; 
     try { 
      jsonArray = new JSONArray(jsonData); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     ArrayList<image_data> myJSONArray = new ArrayList<image_data>(); 

     for (int i=0; i<jsonArray.length(); i++) { 
       **//This is where I think I create the seperate arrays of id name thumb and path** 
      myJSONArray.add(new image_data()); 

     } 


GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this,image_data)); 

    } 
} 
+0

顯示JSON輸出或至少它的一部分來表示什麼對象需要被解析。 –

+0

編輯:添加了JSON數據。 – Denoteone

+0

將JSON數據安排爲另一種方式可能更有意義 - 數組對象,而不是數組對象。 –

回答

4

你如何進行迭代,並添加

for(int i=0;i<jsonArray.length;i++) 
    {  
     JSONObject json_data = jArray.getJSONObject(i); 
     imgnfo.id = json_data.getInt("id_key"); 
     imgnfo.name = json_data.getString("name_key"); 
     imgnfo.thumb = json_data.getString("thumb_key"); 
     imgnfo.info = json_data.getString("info_key"); 
     myArray.add(new image_data()); 
    } 

只需添加適當的鍵名。我不知道他們。

+0

謝謝我看到有很多方法可以做同樣的事情。 +1 – Denoteone

2

呦應該張貼什麼JSON數據的樣子。 假設JSON數據看起來像

...,{ 
    "image_data": { 
     "name": "image name","thumb":"thumbpath","path":"path"} 
},{ 
    "image_data": { 
     "name": "image name 2","thumb":"thumbpath 2","path":"path 2"} 
} 

,那麼你會提取值,像這樣:

ArrayList<image_data> imageArray = new ArrayList<image_data>(); 

     for (int i=0; i<jsonArray.length(); i++) { 
      image_data img = new image_data(); 

      Object jo = jsonArray.get(i); 

      image_data.name = jo.name; 
      image_data.thumb = jo.thumb; 
      image_data.path = jo.path; 

      imageArray.add(new image_data()); 

     } 

考慮使用的getter你類IMAGE_DATA。 還要考慮其重命名爲的ImageData

+0

對不起,我編輯了我的帖子並添加了json佈局。 +1感謝您的帖子有幫助。 – Denoteone