在响应中我期望一个 JSON 字符串。 但现在我必须为我想要检索的对象提供一个 key 。我想要的响应只是简单的 {"xx":"xx","xx":"xx"} 格式,没有像这样的名称的数组 {"XX":["xx":"xx"]} 。如何在不提供参数的情况下获取 JSON。简而言之,我只想读取得到的 JSON 响应,而无需给出参数。
protected JSONObject parseJSONMeth() {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
JSONObject jo = users.getJSONObject(0);
return jo;
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
最佳答案
为什么不使用GSON https://sites.google.com/site/gson/gson-user-guide :
Gson gson = new GsonBuilder().create();
User result = new User();
result=(gson.fromJson(json, User .class));
对于用户列表:
private static List<User> getDataFromJson(String json) {
Gson gson = new GsonBuilder().create();
List<User> result = null;
try {
JSONObject posts=new JSONObject(json);
result = gson.fromJson(posts.toString(), new TypeToken<List<User>>(){}.getType());
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
关于Java volley空json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563867/