@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://demo.cogzideltemplates.com/client/sedio/feed/home_feed?user_id=5&start=1");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("worldpopulation");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("rank", jsonobject.getString("rank"));
map.put("country", jsonobject.getString("country"));
map.put("population", jsonobject.getString("population"));
map.put("flag", jsonobject.getString("flag"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
这是我的代码,但这不适合我,因为我的 json没有顶级元素我怎么能运行它
给我一些例子
答案 0 :(得分:0)
它是这样的:
String s ="[{ \"text\": \"Pain Intensity\", \"question_number\": 1, \"id\": 1}, { \"text\": \"Personal Care (washing, dressing, etc.)\", \"question_number\": 2, \"id\": 2}, { \"text\": \"Lifting\", \"question_number\": 3, \"id\": 3}, { \"text\": \"Walking\", \"question_number\": 4, \"id\": 4}, { \"text\": \"Sitting\", \"question_number\": 5, \"id\": 5}]";
try {
JSONArray array = new JSONArray(s);
for (int i =0 ,l=array.length() ; i<l; i++) {
JSONObject jsonObject = array.getJSONObject(i);
String text = jsonObject.getString("text");
int qNumber = jsonObject.getInt("question_number");
int id = jsonObject.getInt("id");
//handle here the variables ...
}
} catch (JSONException e) {
e.printStackTrace();
}