所以我已导入import org.json.*,它似乎无法识别JSONParser.
String filePath = "C://CN//jokes.json";
try {
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
System.out.println(jsonObject);
}catch (Exception e) {
System.out.println(e);
}
}
Run Code Online (Sandbox Code Playgroud)
jokes.json与http://api.icndb.com/jokes具有相同的信息我希望能够从该站点获取数据,但要测试我已经创建了一个文件.这是我第一次使用Json,所以我有点无能为力.我已经更新了jdk和jre,但我仍然有同样的问题.这是一个屏幕截图,显示它的外观: 截图我也阅读了类似的帖子链接,但它与我的问题不同.
如果你看截图,它似乎import org.json.*没有类JSONParser.我也试过添加,import org.json.simple.parser.JSONParser;但它不承认它.因为"简单"不存在
Jav*_*mer 17
对于Android Studio,只需将其添加到build.gradle依赖项:
compile 'com.googlecode.json-simple:json-simple:1.1'
Run Code Online (Sandbox Code Playgroud)
有关其他IDE和其他选项,请参见此处:http: //mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1
小智 6
另一个答案有点过时了。(在 2018 年底编译将是 api 或实现,还有一个新版本 1.1.1)
将此添加到 build.gradle 依赖项:
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
Run Code Online (Sandbox Code Playgroud)
然后导入
import org.json.simple.parser.JSONParser;
Run Code Online (Sandbox Code Playgroud)
希望它对我有帮助:)