android 工作室无法解析导入 org.json.JSONObject

android studio cannot resolve import org.json.JSONObject

我使用 libgdx 构建我的项目,但在使用 JSONObject class 时遇到问题。当我添加 import org.json.JSONObject 时,它说无法解析。如何将该库添加到我的项目中?

这是我尝试过但没有成功的方法:

  1. 我下载了 json-simple-1.1.jar 并将其放在 core/build/libs 文件夹中。也无法使用 import.org.json.simple.JSONObject
  2. 在 Android Studio 中以 "Packages" 下拉菜单浏览树时,我可以在 "Core -> Libraries -> org -> json" 下看到我想要的 classes 但我无法将它们添加到我的项目中.我收到 "cannot refractor, class is in a jar file" 错误。

有什么我还没有尝试解决这个问题的吗?当我尝试使用其他外部库时,我觉得我会 运行 再次进入这个。

谢谢。

如果您使用的是 LibGdx,它不会附带 Gradle 设置,从而简化多项目构建。

您需要将 implementation 'org.json:json:<version>' 添加到顶级 build.gradle 的依赖块中。

要查找最新版本,请参阅 http://mvnrepository.com/artifact/org.json/json/

例如,在撰写本文时使用最新版本:

dependencies {
   implementation 'org.json:json:20200518'
}