繁体   English   中英

如何从存储在 JSON 文件中的 JSON 数组(来自 API)检索数据?

[英]How can I retrieve data from a JSON array (that came from an API) that I have stored into a JSON file?

提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文

我已经完成的是使用文件编写器将数据从 API 复制到它自己的 JSON 文件 Courses.json 和 package com.google.gson。 总的来说,我需要检索的数据是 enrollments 下的 user_id。

Courses.json 的缩短和编辑版本如下:

[
  {
    "access_restricted_by_date":true,
    "id":3155
  },
  {
    "access_restricted_by_date":true,
    "id":4604
  },
  {
    "access_restricted_by_date":true,
    "id":4655
  },
  {
    "root_account_id":1,
    "storage_quota_mb":50000,
    "template":false,
    "end_at":null,
    "public_syllabus_to_auth":false,
    "public_syllabus":false,
    "created_at":"2022-08-11T18:00:41Z",
    "start_at":null,
    "enrollment_term_id":180,
    "uuid":"MC24SbWdGDjeKTalLT5T5V41Foh0jD4EybOFG8xY",
    "course_code":"Study Hall",
    "course_color":null,
    "grading_standard_id":null,
    "workflow_state":"available",
    "id":6312,
    "homeroom_course":false,
    "default_view":"assignments",
    "is_public_to_auth_users":false,
    "grade_passback_setting":null,
    "enrollments":[
      {
        "role":"StudentEnrollment",
        "enrollment_state":"active",
        "role_id":3,
        "user_id":9999,
        "type":"student",
        "limit_privileges_to_course_section":false
      }
    ],
    "blueprint":false,
    "license":"private",
    "is_public":false
    }
]

但是,无论我尝试做什么,我都无法获得读取数组的功能代码。

我试过将 json-simple 与 fileReader 一起使用,但我所做的一切都不起作用。

尝试使用基本代码来读取“id”:

import java.io.*;
import com.google.gson.*;
import com.google.gson.stream.*;
import java.io.IOException;
public class CourseGradeReader {
    public static void main(String args[]) {
        JsonReader reader;
        try {
            reader = new JsonReader(new FileReader("Courses.json"));
            reader.beginArray();
            while(reader.hasNext()) {
                String name = reader.nextName();
                if(name.equals("id")) {
                    System.out.println(reader.nextString());
                    reader.endArray();
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
            reader.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}

The error that results from this code is java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:386) at org.example.CourseGradeReader.主要(课程成绩阅读器.java:11)

彻底放弃GSON,改用org.json.simple

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileReader;
import java.io.IOException;

public class CourseGradeReader {
public static void main(String args[]) throws IOException, ParseException {
    JSONParser parser = new JSONParser();
    JSONArray a = (JSONArray) parser.parse(new FileReader("Courses.json"));
    for (Object o : a) {
        JSONObject course = (JSONObject) o;

        long id = (long) course.get("id");
        System.out.println("Course Id: " + id);

        String created_at = (String) course.get("created_at");
        if(!(created_at == null)) {
            System.out.println(created_at);
        }

        JSONArray enrollments = (JSONArray) course.get("enrollments");
        if(!(enrollments == null)) {

            for (Object c : enrollments) {
                JSONObject enrollment_list = (JSONObject) c;

                long user_id = (long) enrollment_list.get("user_id");
                System.out.println("User:" + user_id);

            }
        }
        System.out.println("============================");
    }
}

}
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM