import org.apache.commons.io.FileUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class ReadJSONSample {
public static void main(String[] args) throws IOException, JSONException {
File file = new File(ReadJSONSample.class.getResource("/wangxiaoer.json").toString());
String content = FileUtils.readFileToString(file);
JSONObject jsonObject = new JSONObject(content);
System.out.println("姓名是:"+jsonObject.getString("name"));
}
}
控制台运行后报错
Exception in thread "main" java.lang.NullPointerException
at json.ReadJSONSample.main(ReadJSONSample.java:13)
可能是wangxiaoer.json的路径不对。
wangxiaoer.json文件应该 与ReadJSONSample.class文件在同一个目录下 。
注意:不是.java类型的文件,而是.class类型。
.class文件一般在bin目录下,具体在哪儿还得在你的项目中找找。