首页 > 解决方案 > org.json.simple.JSONArray 无法在 com.tpg.json.JsonParsing.main(JsonParsing.java:24) 处转换​​为 org.json.simple.JSONObject

问题描述

我正在尝试从以下 JSON 文件中读取用户 ID 和用户角色:

[ https://drive.google.com/file/d/1X6t2kKaYArwM-mLc4EQhPAfUWF95RYTW/view?usp=sharing][1]

我在同一个包中添加了 erp.json 和类文件:com.tpg.json

我正在使用 json 简单的 jar 文件。

并面临错误: org.json.simple.JSONArray 无法转换为 org.json.simple.JSONObject

你能建议我编辑并重新发布代码吗?

您也可以发布您的代码或其他代码,这将为我提供所需的输出。

 package com.tpg.json;

import java.io.File;
import java.io.FileReader;

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

//com.googlecode.json-simple
public class JsonParsing {

    public static void main(String[] args) {
        ClassLoader classLoader = new JsonParsing().getClass().getClassLoader();
        String fileName = "com/tpg/json/erp.json";
        File file = new File(classLoader.getResource(fileName).getFile());

        JSONParser parser = new JSONParser();
        try {
            FileReader reader = new FileReader(file.getAbsolutePath());
            Object obj = parser.parse(reader);
            JSONObject jsonObj = (JSONObject) obj;

            JSONObject res = (JSONObject) jsonObj.get("Resources");
            System.out.println("studentDetails :" + res.toJSONString());

            JSONArray ID = (JSONArray) res.get("id");
            System.out.println("Id" + ID);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

标签: javajsonhttp

解决方案


推荐阅读