Android 如何从json数组中获取整数值
下面给出了我的Android 如何从json数组中获取整数值,android,json,Android,Json,下面给出了我的json的响应,我正试图解析数据,但当我解析用户照片id时,它给出了JSONException:没有用户照片id的值,有人能告诉我吗 { "user_login_id": "2650", "user_total_photo": "3", "max_upload_photo": "3", "image_list": [ { "user_photo_id": 334, "status": "Approved",
json
的响应,我正试图解析数据
,但当我解析用户照片id
时,它给出了JSONException:没有用户照片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();
} */
}
}
class LoadImages扩展了异步任务{
弦类光体;
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(getActivity());
设置消息(“加载…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的ArrayList doInBackground(字符串…args){
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
ArrayList数据=新的ArrayList();
字符串jsonStr=sh.makeServiceCall(IMAGE\uURL,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
试一试{
jsonObj=新的JSONObject(jsonStr);
//创建新的HashMap
HashMap=newHashMap();
//将每个子节点添加到HashMap key=>value
put(IMAGE\u USERLOGIN\u ID,jsonObj.getString(IMAGE\u USERLOGIN\u ID));
put(IMAGE_TOTAL_PHOTO,jsonObj.getString(IMAGE_TOTAL_PHOTO));
put(IMAGE\u MAX\u UPLOAD,jsonObj.getString(IMAGE\u MAX\u UPLOAD));
/*put(IMAGE\u USER\u PHOTOID,jsonObj.getString(IMAGE\u USER\u PHOTOID));
put(IMAGE_STATUS,jsonObj.getString(IMAGE_STATUS));
put(IMAGE_PHOTO,jsonObj.getString(IMAGE_PHOTO));
*/
image_list=(JSONArray)jsonObj.get(image_列表);
对于(int i=0;i
希望这有帮助。您只需要遵循json字符串的结构。(json数组包含json对象,因此首先在循环中获取json对象,然后从中获取用户\u photo\u id)
image_list=(JSONArray)jsonObj.get(“image_list”);
对于(int i=0;i
向其他活动发送json数据的更新:
1.创建一个类来存储数据,如下所示:
public class JsonData{
private JsonObject mData;
//getter and setter and other function you need
}
2.实现可序列化:
public class JsonData implements Serializable{
private static final long serialVersionUID = 6037391814333931008L;//It should generate by your IDE
private JsonObject mData;
//getter and setter and other function you need
}
3.启动活动时,使用Bundle和Intent发送可序列化数据
Intent intent = new Intent(Sender.this, Receiver.class); // replace Sender and Receiver with your Activities
Bundle bundle = new Bundle();
bundle.putSerializable("Your Key", yourDataClass);//replace yourDataClass with your JsonData class
intent.putExtras(bundle);
startActivity(intent);
4.在其他活动中接收数据:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Serializable data = bundle.getSerializable("Your Key");
if (data != null) {
JsonData jsonData= (JsonData) data;
//now you have got your data and do whatever you want.
}
}
在创建此活动时添加代码:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Serializable data = bundle.getSerializable("Your Key");
if (data != null) {
JsonData jsonData= (JsonData) data;
//now you have got your data and do whatever you want.
}
}
但是,如果活动是singleTask
模式,则步骤4中的代码也应置于onNewIntent中。在这种情况下,您应该将意图保存在onNewIntent
中:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
//here to place the code in Step 4 and also place in onCreate function
}
只需编写jsonObj.getInt(IMAGE\u USERLOGIN\u ID);我不想要用户登录id,我想要用户照片id只要写下这个jsonObj.getInt(“用户照片id”);mage_list=(JSONArray)jsonObj.getInt(IMAGE_USER_PHOTOID);我试过这样做..它显示无法从int强制转换到JSONArraycan你能给我看解析代码吗?如果我需要将数组中的所有对象发送到下一个活动怎么办?你应该创建一个包含你要发送的数据的类,让这个类实现
可序列化。还有要发送的代码:Intent Intent=new Intent(…,…);Bundle Bundle=new Bundle();Bundle.putSerializable(“key”,yourDataClass);intent.putExtras(Bundle);startActivity(intent);我认为这里不应该写太多代码
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Serializable data = bundle.getSerializable("Your Key");
if (data != null) {
JsonData jsonData= (JsonData) data;
//now you have got your data and do whatever you want.
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
//here to place the code in Step 4 and also place in onCreate function
}