我正在尝试解析从网络数据库中获得的响应。我只得到一个值,即卖家 ID 作为回应。当我尝试在 JSON 对象中传递它时,不知何故在它没有执行之后的 toast 和它没有执行之后的 toast 。
我知道有很多相关问题,但我找不到问题所在...请帮忙。谢谢。
try {
JSONArray jArray = new JSONArray(stuff.toString());
Toast.makeText(this, "len: " + jArray.length(), 1000).show(); // length is 1
for (int i = 0; i < jArray.length(); i++) {
Toast.makeText(this, "Arr: " + stuff, 1000).show();
// Arr: [{"seller_id":"5"}]
JSONObject jObject = jArray.getJSONObject(i);
j_id = jObject.getString(stuff);
// not getting executed
Toast.makeText(this, "JSON: " + j_id, 1000).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(this, "View: " + j_id, 1000).show(); // j_id is giving null value
这是我的 json 数据 [{"seller_id":"5"}] 我试图从中获取卖家 ID 的值(即此处为 5)。
最佳答案
jObject.getString() 方法参数有问题
JSONObject jObject = jArray.getJSONObject(i);
j_id = jObject.getString(stuff);
你给数组。它应该是对象的名称,即 seller_id
所以在修改之后你的代码将是 j_id = jObject.getString("seller_id");
关于android - JSON 解析返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15872339/