gpt4 book ai didi

android - 如何从 json 数组中获取整数值

转载 作者:行者123 更新时间:2023-11-29 20:56:14 27 4
gpt4 key购买 nike

下面给出了我的 json 的响应,我正在尝试解析数据,但是当我解析 user_photo_id 时,它给出了 JSONException:No values for user_photo_id,谁能告诉我?

{  
"user_login_id": "2650",
"user_total_photo": "3",
"max_upload_photo": "3",
"image_list": [
{
"user_photo_id": 334,
"status": "Approved",
"photo": ""
},
{
"user_photo_id": 394,
"status": "Approved",
"photo": ""
},
{
"user_photo_id": 398,
"status": "Admin Approve Remaining",
"photo": ""
}
]
}

我的java代码:

 class LoadImages extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {


String photoid;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();

// Making a request to url and getting response
ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(IMAGE_URL, ServiceHandler.GET);

Log.d("Response: ", "> " + jsonStr);


try {
jsonObj = new JSONObject(jsonStr);


// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(IMAGE_USERLOGIN_ID, jsonObj.getString(IMAGE_USERLOGIN_ID));
map.put(IMAGE_TOTAL_PHOTO,jsonObj.getString(IMAGE_TOTAL_PHOTO));
map.put(IMAGE_MAX_UPLOAD, jsonObj.getString(IMAGE_MAX_UPLOAD));
/*map.put(IMAGE_USER_PHOTOID, jsonObj.getString(IMAGE_USER_PHOTOID));
map.put(IMAGE_STATUS, jsonObj.getString(IMAGE_STATUS));
map.put(IMAGE_PHOTO, jsonObj.getString(IMAGE_PHOTO));
*/




image_list = (JSONArray) jsonObj.get(IMAGE_LISTING);
for(int i=0;i< image_list.length();i++)
{
photoid += image_list.get(i);
Log.d("mylog", "initial marital = " + photoid);

}

getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{
Intent intent=new Intent(getActivity(),PhotoView.class);
intent.putExtra("id", id);
intent.putExtra("totals", totalphota);
intent.putExtra("maxs", maximumphota);

startActivity(intent);
}
});
/* image_list = (JSONArray) jsonObj.get("user_photo_id");
image_list=(JSONArray) jsonObj.get(IMAGE_STATUS);
image_list=(JSONArray)jsonObj.get(IMAGE_PHOTO);
for(int i=0;i< image_list.length();i++)
{
String photoid=

}*/

} catch (JSONException e) {
e.printStackTrace();
}


return data;
}


protected void onPostExecute(ArrayList<HashMap<String,String>> result) {

super.onPostExecute(result);


// dismiss the dialog after getting all albums
if (pDialog.isShowing())
pDialog.dismiss();
// updating UI from Background Thread
/*Intent mIntent = new Intent(getActivity(),Filter.class);
Bundle extras = mIntent.getExtras();
try {
extras.putString("radiostatus", jsonObj.getString(FILTER_ALLOW));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */


}

}

最佳答案

希望对您有所帮助。您只需要遵循 json 字符串的结构。 (json 数组包含 json 对象,因此首先在循环中获取 json 对象,然后从中获取 user_photo_id )

image_list = (JSONArray) jsonObj.get("image_list");
for(int i=0;i< image_list.length();i++)
{
JSONObject imageListItem = image_list.getJSONObject(i);
int userPhotoId = imageListItem.getInt("user_photo_id")
Log.d("mylog", "i ="+i+" and user_photo_id =" + userPhotoId);
}

关于android - 如何从 json 数组中获取整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27644703/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com