我对android / java有点新意。我试图将JSON值传递到列表中,然后传递到多维数组中。我没有太大的成功。
2个问题, 1)如何将json数组中的所有变量加载到children [] []中? 2)如何在Log.i
中查看子项[] []这是我的代码:
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 :(得分: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数据格式正确,以避免异常。 要查看子项[] [],您可以在多维数组上迭代两次并执行Log.i(“MyTag”,“Value:”+ children [i] [j]);
答案 1 :(得分: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);