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

mbskvtky  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(162)

我所做的是使用filewriter和com. google. gson包将数据从API复制到它自己的JSON文件Courses.json中。总的来说,我需要检索的数据是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
    }
]

然而,无论我尝试做什么,我都无法获得读取数组的功能代码。
我尝试过在fileReader中使用json-simple,但没有任何效果。
尝试让基本代码只读取“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();
        }
    }

}

此代码导致的错误是java.lang.IllegalStateException:预期为开始_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY。网址为com。谷歌。gson。流。网站为jsonReader。开始对象(jsonReader.java:386),网址为org。示例。课程成绩阅读器。主(课程成绩阅读器。java:11)

nzkunb0c

nzkunb0c1#

完全放弃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("============================");
    }
}

}

相关问题