使用Java读取json文件时出现java.lang.ClassCastException [英] java.lang.ClassCastException when reading the json file using java

查看:500
本文介绍了使用Java读取json文件时出现java.lang.ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java读取一个名为(results.json)的.json文件,并通过键'title'提取数组的值.

I want to read a .json file called (results.json) using java and extract the values of the array by the key 'title'.

这是我的.json文件

This is my .json file

 [
    {
        "title": {
            "0": "UNIQUE SIGNED HARRY POTTER DELUXE VOLUME SALESMAN DUMMY"
        }
    },
    {
        "title": {
            "0": "Harry Potter and the Philosopher's Stone by JK Rowling - Uncorrected Proof/ARC!!"
        }
    },
    {
        "title": {
            "0": "Huge Lego Lot 532 Lbs Pounds Legos Star Wars Castle Harry Potter City Minifigs"
        }
    }
]

这是我正在使用的Java代码

This is the java code i'm using

public class JJ {

public static void main(String[] args)
{        
    readJsonFile();
}

public static void readJsonFile() {

    BufferedReader br = null;
    JSONParser parser = new JSONParser();

    try {

        String sCurrentLine;

        br = new BufferedReader(new    FileReader("C:/wamp/www/epsilon/results.json"));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println("Names of the Books:\t" + sCurrentLine);

            Object obj;
            try {

                obj = parser.parse(sCurrentLine);
                JSONObject jsonObject = (JSONObject) obj;
                String name = (String) jsonObject.get("title");

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
}

我说错了

线程"main"中的异常java.lang.ClassCastException:org.json.simple.JSONArray无法转换为org.json.simple.JSONObject.

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject.

推荐答案

这很简单.您的Json文件在顶层具有一个数组.当JSonParser解析它时,它将作为JSONArray返回.您正尝试将其强制转换为JSONObject(类似于地图或字典.)您需要做的是:

It's pretty simple. Your Json file has an array at the top level. When the JSonParser parses it, it's returning it as a JSONArray. You're trying to cast it to a JSONObject instead (which is like a map, or dictionary.) What you need to do is this:

Object obj;
try {

    obj = parser.parse(sCurrentLine);
    JSONArray jsonArray = (JSONArray) obj;
    for(obj : jsonArray){//not sure of the exact syntax, I don't have an IDE in front of me.
        JSONObject jsonObject = (JSONObject)obj;
        JSONObject realTitle = (JSONObject)jsonObject.get("0");
        String name = (String) realTitle.get("title");
    }


} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

这篇关于使用Java读取json文件时出现java.lang.ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆