我正在通过查询URL获取JSON数据
http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors但是当我解析时,我只得到了第一组数据,而不是第二组数据,不确定我在哪里出错了。如何从JSON数组中获取所有节点。谢谢你的帮助。
下面是我的代码:
/**
* getting Inbox JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
url = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors"
Log.d(TAG , "URL IS:" + url);
JSONObject json = jsonParser.makeHttpRequest(url, "GET", params, false);
Log.d("Parse", "Data : " + json.toString());
try {
data = json.getJSONArray("actors");
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
//Here I am getting all values one by one
}
} catch (JSONException e) {
e.printStackTrace();
} catch(Exception e){
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
}
}发布于 2016-04-21 18:32:07
实际上,您必须将所有数据分别设置为一些全局变量,然后在for循环中使用。尝试以下代码:
ArrayList<MyListEntity> subList;
for (int i = 0; i < data.length(); i++) {
//get the information JSON object
String subIDInfo = objSub.getJSONObject(i).toString();
//create java object from the JSON object
MyListEntity cat = gson.fromJson(subIDInfo, MyListEntity.class);
//add to country array list
subList.add(cat);
}这里MyListEntity是全局variables.In的类,你必须从JSON获取并设置你想要的变量。
发布于 2016-04-21 17:45:16
因为在for循环中,您不会对已解析的值做任何操作。在迭代循环之前,您需要将该值存储在某个地方。
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String weekId = null;
weekId = c.getString("weekid"); //store this value in a String array or ArrayList
}发布于 2016-04-21 17:50:57
ArrayList<String> weekId;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String weekId = null;
weekId.add(c.getString("weekid"));
}https://stackoverflow.com/questions/36765673
复制相似问题