java - 我无法从 JSON 数组中获取对象
问题描述
我想在actor中获取所有对象,我该怎么做?
private void parseJSON() {
String url = "https://api.github.com/events";
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray jsonArray = response.getJSONArray("actor");
for(int i =0; i <jsonArray.length();i ++){
JSONObject act = jsonArray.getJSONObject(i);
int id = act.getInt("id");
String imageUrl = act.getString("avatar_url");
String login = act.getString("login");
String displaylogin = act.getString("display_login");
String gravatar_id = act.getString("gravatar_id");
String url = act.getString("url");
解决方案
actor
不是数组。
你可以读到actor
类似的东西
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JacksonExample2 {
private static final String jsonStr = "YOUR JSON";
public static void main(String[] args) throws JSONException {
JSONArray jsonArray = new JSONArray(jsonStr);
for(int i=0;i<jsonArray.length();i++) {
JSONObject item = (JSONObject) jsonArray.get(i);
JSONObject actor = (JSONObject) item.get("actor");
// You can read all properties of actor like 'actor.getString("avatar_url");'
System.out.println(actor);
}
}
}
推荐阅读
- javascript - NodeJS在发送请求并设置间隔以重复该功能后,我更新了我的代码源,但我的代码的代码源响应没有更新
- mongodb - 与 MongoDB 中的多用途实体的不同实体关联?
- mysql - 无法创建表`cms`.`invoice`(errno:150“外键约束格式不正确”)
- xslt - 如何在 XSLT 中使用 group-by 标记化后删除相同的数字
- javascript - 将 jquery 版本从 v2.1.3 更新到 v3.5.1 时需要考虑的事项
- android - 使用 Kotlin Coroutines 更新我的 TextView 会导致它崩溃:
- azure-data-factory - ADF SQL 链接服务连接失败
- javascript - 如何检查无效的用户 ID 和未禁止的用户
- keras - 用于视频输入的 LSTM
- docker - 运行 docker-compose build 后获取构建文件以在主机上持久保存