2012-03-30 41 views
0

我是一個新的android/java。我正在嘗試將JSON值傳遞到列表中,然後傳遞到多維數組中。我沒有太大的成功。如何將結果從JSON數組傳遞到多維數組中?

2個問題, 1)如何將一個json數組中的所有變量加載到children [] []中? 2)你如何看待孩子[] []在Log.i

Herei是我的代碼:

List<String> cList = new ArrayList<String>(); 
String customer_name, customer_title, customer_postal_code, customer_city, customer_state, customer_street_address; 
ArrayList<String> cTitle, cClubName, cPostalCode, cCity, cState, cStreet = new ArrayList<String>();  
public String[][] children = null; 

// ... onCreate方法,HTTP連接,StringBuilder的,等等,這些做工精細... //將數據傳遞到陣列

try{ 
    JSONArray jArray = new JSONArray(result); 
    JSONObject jData=null; 


String[][] children = new String[jArray.length()][6]; 

    for(int i=0;i<jArray.length();i++){ 


      jData = jArray.getJSONObject(i); 

      customer_name=jData.getString("customer_name");  
      Log.i("JSON ", "customer_name LOG " + customer_name); 
      cList.add(customer_name); 

      customer_title=jData.getString("event_title"); 
      Log.i("JSON ", "customer_title LOG " + customer_title); 
      cList.add(customer_title); 

      customer_street_address=jData.getString("customer_street_address"); 
      Log.i("JSON ", "customer_Id LOG " + customer_street_address); 
      cList.add(customer_street_address); 


      customer_city=jData.getString("customer_city"); 
      Log.i("JSON ", "customer_city LOG " + customer_city); 
      cList.add(customer_city); 



      customer_state=jData.getString("customer_state"); 
      Log.i("JSON ", "customer_state LOG " + customer_state); 
      cList.add(customer_state); 


      customer_postal_code=jData.getString("customer_postal_code");    
      Log.i("JSON ", "customer_postal_code LOG " + customer_postal_code); 
      cList.add(customer_postal_code); 

      for(int ic = 0; ic < cList.size(); ic++) {  
       Log.i("jData ", "length " + jData.length()); 

       children[i][ic] = (String) cList.get(ic); 

      } 

      Log.i("Child Array", "Children array LOG " + children); 




    } 


    }catch(JSONException e1){ 
     Toast.makeText(getBaseContext(), "No customers Found", Toast.LENGTH_LONG).show(); 
    }catch (ParseException e1){ 
     e1.printStackTrace(); 
    } 

}

+0

你可以得到一個JSON的例子嗎?我會盡力讓你更加靈活和好看的解決方案 – 2012-03-30 08:16:39

回答

1

如果我正確理解你的代碼,你不需要cList。 類似的東西應該做的工作

String[][] children = new String[jArray.length()][6]; 

    for(int i=0;i<jArray.length();i++){ 

      jData = jArray.getJSONObject(i); 

      customer_name=jData.getString("customer_name");  
      Log.i("JSON ", "customer_name LOG " + customer_name); 
      children[i][0] = customer_name; 


      customer_title=jData.getString("event_title"); 
      Log.i("JSON ", "customer_title LOG " + customer_title); 
      children[i][1] = event_title; 

      customer_street_address=jData.getString("customer_street_address"); 
      Log.i("JSON ", "customer_Id LOG " + customer_street_address); 
      children[i][2] = customer_street_address; 

      customer_city=jData.getString("customer_city"); 
      Log.i("JSON ", "customer_city LOG " + customer_city); 
      children[i][3] = customer_city; 

      customer_state=jData.getString("customer_state"); 
      Log.i("JSON ", "customer_state LOG " + customer_state); 
      children[i][4] = customer_state; 


      customer_postal_code=jData.getString("customer_postal_code");    
      Log.i("JSON ", "customer_postal_code LOG " + customer_postal_code); 
      children[i][5] = customer_postal_code; 
    } 

確保您的JSON數據很好地形成,以避免例外。你可以在你的multidimentional數組上迭代兩次,然後做log.i(「MyTag」,「Value:」+ children [i] [j]);

0
String[][] data = new String[jsonArray.length][]; 

for(int i = 0; i<jsonArray.length; i++){ 
    data[i] = ((ArrayList)jsonArray).get(i).toArray(new String[((ArrayList)jsonArray).get(i).size]) 
} 

用於打印在日誌中

Log.d("array", data);