我在嘗試從json文件 中讀取我的java類時出現問題; s向我顯示此錯誤: 注意:我安裝了json-simple並使用它succeufuly解析方法(FileReader)對於JSONParser類型是未定義的
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method parse(FileReader) is undefined for the type JSONParser
at com.cd.JSONParser.main(JSONParser.java:18)
這是我的代碼:
package com.cd;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JSONParser {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("g:\\testm1.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");
System.out.println(name);
long age = (Long) jsonObject.get("age");
System.out.println(age);
// loop array
JSONArray msg = (JSONArray) jsonObject.get("messages");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
非常感謝你 –