java 使用java读取json文件时出现java.lang.ClassCastException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16133162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ClassCastException when reading the json file using java
提问by posha
I want to read a .json file called (results.json) using java and extract the values of the array by the key 'title'.
我想使用 java 读取一个名为 (results.json) 的 .json 文件,并通过键“title”提取数组的值。
This is my .json file
这是我的 .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"
}
}
]
This is the java code i'm using
这是我正在使用的 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();
}
}
}
}
I'm getting an error saying
我收到一条错误消息
Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject.
线程“main”中的异常 java.lang.ClassCastException: org.json.simple.JSONArray 无法转换为 org.json.simple.JSONObject。
回答by Kylar
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:
这很简单。您的 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();
}
回答by Rob
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");
}
回答by user2167877
I had same issue, to solve it just replace this:
我有同样的问题,要解决它只需替换这个:
obj = parser.parse(sCurrentLine);
JSONObject jsonObject = (JSONObject) obj;
by:
经过:
obj = parser.parse(sCurrentLine);
JSONObject jsonObject = new JSONObject((parser.parse(sCurrentLine)).toString());