android - 如何一次获取 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();
}
解决方案
您可以使用 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) {
}
推荐阅读
- amazon-web-services - 获取 AWS 安全组规则 ID
- node.js - 从 pcap NodeJS 解密 SSL 数据
- r - 有没有办法在 R 中添加两个连续列?
- python - 不允许我将整数放入变量中(python 以下教程的新手)
- pyspark - 如何使用结构化流从数据库中读取传入数据
- seaborn - Seaborn 热图仅适用于列的子集
- azure - Power BI 和 Azure 密钥保管库
- jquery - ASP.NET MVC:关闭后对话框不起作用
- java - 有没有办法使用 Regex 在 Mongo 上执行 indexOps?
- r - 使用类似名称的主文件重命名 Multi-FASTA 序列标题