我想使用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。