我想使用JsonParsing获取countdata。这怎么可能? 我的json回复是,
{"data":[{"l_id":"1","p_id":"5","user_id":"15"},{"l_id":"2","p_id":7","user_id":"16"}]}{"countdata":2}
countdata是数据总数。我能够获取数据但不能计数数据。请指导我。
我能够成功获取i_id,p_id和user_id的数据,而我的代码是,
private class GetDetail extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
dataList = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONFunctions.getJSONfromURL(urlData);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
String lId=jsonobject.getString(TAG_L_ID);
String pId=jsonobject.getString(TAG_P_ID);
String userId=jsonobject.getString(TAG_User_ID);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
但我无法获取countdata的数据。并给出错误,java.lang.IndexOutOfBoundsException:索引2无效,大小为0
我的代码是,
private class getData extends AsyncTask<Void, Void, Void> {
JSONObject jsonobject;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
arr = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONFunctions.getJSONfromURL(urlCount);
String strCount = jsonobject.getString("qdata");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
请指导我。 提前谢谢。
答案 0 :(得分:2)
首先,你的json响应无效
有效的json:
{"data":[{"l_id":"1","p_id":"5","user_id":"15"},{"l_id":"2","p_id":"7","user_id":"16"}],"countdata":"2"}
如何获取countdata:
String strCount = jsonobject.getString("countdata");