我一直想知道如何在 android 中解析 JSON 数组?
我有一个像这样的 json 对象:[{"ID":"1","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="07626a666e6b476a666e6b296a666e6b" rel="noreferrer noopener nofollow">[email protected]</a>","password":"password"},{"ID":"2","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="344051474074405147401a575b59" rel="noreferrer noopener nofollow">[email protected]</a>","password":"passward"}]
但我找不到如何解析它,我想访问数据,这样我就可以获得第一封邮件或第二个 ID。
我尝试了大约 40 种不同的解决方案,但没有成功。
上次我尝试的东西是这样的:
private void showJSON(String json){
try {
JSONArray jArray = new JSONArray(json);
String JsonString = jArray["ID"];
tv.append(JsonString);
} catch (JSONException e) {e.printStackTrace();}
}
但它需要一个数组而不是 json 数组。我还没找到如何将其转换为数组。
我也尝试过这个:
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("email");
}
但是 getJSONObject 不存在。
感谢您的帮助。
最佳答案
试试这个
private void showJSON(String json){
try{
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
String id[i] = jsonArray.getJSONObject(i).getString("id");
String email[i] = jsonArray.getJSONObject(i).getString("email");
}
}
catch (JSONException e) {
// Log.d("JSONException", e.toString());
}
}
关于java - 如何在android中解析简单的JSON数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34622591/