JSONArray jsonArray = jsonObject.optJSONArray("data");
// array = dataObj.getJSONArray("data");
//JSONArray array = dataObj.getJSONArray("result");
if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0;i <= jsonArray.length() - 1; i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String key = obj.optString("materialId");
String value = obj.optString("materialName");
//System.out.println("key:"+key);
System.out.println("values:"+value);
MaintainType type = new MaintainType();
type.setId(key);
type.setType(value);
types.add(type);
System.out.println(types);
//Logger.d(TAG,);
}
}
一维数组解析
try {
JSONObject jsonObject = new JSONObject(result);
if (jsonObject != null) {
status = jsonObject.optBoolean("status");
if (!status) {
msg = jsonObject.optString("msg");
return;
}
JSONObject dataObj = jsonObject.optJSONObject("data");
JSONArray array = dataObj.names();
if(array != null && array.length() > 0){
for (int i = 0; i < array.length(); i++) {
String key = array.getString(i);
String value = dataObj.optString(key);
BudgetType type = new BudgetType();
type.setId(key);
type.setType(value);
types.add(type);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}