首页 > 解决方案 > 如何一次获取 2 个 json 对象?

问题描述

我正在使用 volley 来获取 json 对象,我正在获取这样的数据

{"resultUser":19} {"resultUser2":13}

如何获得第二个(resultuser2)或两者?

try {
    JSONObject o = new JSONObject(response);
    String data = (String) o.get("resultUser2");
    if (!data.equals("")) {
        Toast.makeText(getApplicationContext(), "user2 id" + data, Toast.LENGTH_LONG).show();
        //UserDetailsActivty.this.finish();
    } else {
        Toast.makeText(getApplicationContext(), "Ohh! Sorry,,Signing Up Failed ", Toast.LENGTH_LONG).show();
    }
} catch (JSONException e) {
    e.printStackTrace();
}

标签: androidjsonpostandroid-volley

解决方案


您可以使用 JsonArray 像这样遍历所有 jsonobjects

try {

JSONArray jsonArray = new JSONArray(response);

for (int i =0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.get(i);
//do whatever you want to do with the data
}
} catch(Exception e) {

}

推荐阅读