使用java读取json文件时的java.lang.ClassCastException

时间:2013-04-21 16:02:03

标签: java json web-services ebay

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

这是我的.json文件

 [
    {
        "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代码

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。

3 个答案:

答案 0 :(得分:2)

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

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();
}

答案 1 :(得分:1)

        Object obj = parser.parse(new FileReader("/Users/path/to/file/file.txt"));
        JSONArray jsonArray = (JSONArray) obj;
        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject jsonObjectRow = (JSONObject) jsonArray.get(i);
            String name = (String) jsonObjectRow.get("Name");
            String address = (String) jsonObjectRow.get("Address");
            Long telephone = (Long) jsonObjectRow.get("Phone_Number");

        }

答案 2 :(得分:-2)

我有同样的问题,解决它只是替换它:

obj = parser.parse(sCurrentLine);
JSONObject jsonObject = (JSONObject) obj;

由:

obj = parser.parse(sCurrentLine);
JSONObject jsonObject = new JSONObject((parser.parse(sCurrentLine)).toString());