2011-10-07 34 views
12

我正在製作一個應用程序,我希望在2個活動之間傳遞json數組。如何通過android中的intents將json arry從一個活動傳遞到另一個活動。任何人都可以幫我解決這個問題嗎? 謝謝將jsonarray從1活動傳遞到另一個

回答

14

將JsonArray轉換爲字符串,然後將其附加到意圖ans發送它。

JSONObject jObject = new JSONObject("Your Json Response"); 

Intent obj_intent = new Intent(Main.this, Main1.class); 
Bundle b = new Bundle();     
b.putString("Array",jObject4.toString()); 
obj_intent.putExtras(b); 

其中jObject4是JSON對象。

獲得下一頁:

Bundle b = getIntent().getExtras(); 
String Array=b.getString("Array"); 
+3

這只是路過的JSONObject,不JSONArray。 – ChihHao

43
Intent intent = new Intent(your_activity.this, new_activity.class); 
intent.putExtra("jsonArray", mJsonArray.toString()); 
startActivity(intent); 

在接下來的活動

 Intent intent = getIntent(); 
     String jsonArray = intent.getStringExtra("jsonArray"); 

     try { 
      JSONArray array = new JSONArray(jsonArray); 
      System.out.println(array.toString(2)); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
+4

解決了我的問題! – SAHIL

+0

intent.putExtra(「jsonArray」,mJsonArray.toString());什麼是mJsonArray在這裏? –

+0

mJsonArray是JSONArray的對象 –

相關問題