我试过了,但在 for 循环 optJSONObject(i) 中出现错误
{
"response": {
"code": 1,
"message": "success"
},
"data": {
"updates": [
{
"id":"67",
"date":"6 months ago",
"update_type": "7",
"update_id": "67",
"name":"ravi"
},
{
"id":"68",
"date":"3 months ago",
"update_type": "5",
"update_id": "68",
"name":"paresh"
},
{
"id":"69",
"date":"1 months ago",
"update_type": "6",
"update_id": "69",
"name":"sampath"
},
{
"id":"62",
"date":"9 months ago",
"update_type": "6",
"update_id": "62",
"name":"raju"
}
]
}
}
我为此尝试的代码是:
try {
InputStream is = null;
is = getApplicationContext().getResources().openRawResource(R.raw.myfile);
//CONVERTS STREAM OBJ IN STRING OBJ
mJSONString = convertStreamToString(is);
JSONObject obj = new JSONObject(mJSONString);
JSONObject dataobj = obj.getJSONObject("data");
JSONObject updateobj = dataobj.getJSONObject("update");
//System.out.println(new JSONObject(update)toString(2));
for(int i=0; i<updateobj.length(); i++){
JSONObject object = updateobj.optJSONObject(i);
mUpdates = new Mobile_UpdateActivity();
mUpdates.update_id = object.getString("update_id");
mUpdates.site_id = object.getString("site_id");
mUpdates.update_type = object.getString("update_type");
System.out.println(mUpdates.update_id);
System.out.println(mUpdates.site_id);
System.out.println(mUpdates.update_type);
}
}
但我在 **optJSONObject(i) 遇到错误
错误:JSONObject 类型中的 optJSONObject(String) 方法不适用于参数 (int)**