【问题标题】:Gradle dependency json-simple errorGradle依赖json-简单错误
【发布时间】:2018-04-08 02:17:34
【问题描述】:

我对 Gradle 还很陌生,所以我正在尝试构建一个 Java 项目,但不确定依赖关系。我从来没有将 Gradle 配置为能够进行我的测试,或者现在是一个 jar 文件来编译和运行。

我的build.gradle

apply plugin: 'java'
apply plugin: 'maven'

repositories {
   jcenter()
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.25'
    compile 'org.json:json:20160212'
    testCompile 'junit:junit:4.12'
}

这就是我在控制台上得到的,说它没有看到我的导入:

 error: package org.json.simple does not exist
 import org.json.simple.JSONParser;

这是我的课:

import org.json.simple.*;
import java.io.*;
import java.util.*;
import java.lang.*;

public class FileLoader {
  @SuppressWarnings("unchecked")
  public static void main(String args[]) {
    JSONParser parser = new JSONParser();
    int count = 0;

    try {
      Object obj = parser.parse(new FileReader(
          "Consumers.json"));

      JSONObject jsonObject = (JSONObject) obj;
      JSONArray array = jsonObject.getJSONArray("people");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

【问题讨论】:

  • 什么版本的 Gradle,你的类是什么样的?你能添加最少的代码来重现问题吗?

标签: gradle junit dependencies build.gradle json-simple


【解决方案1】:

如果您下载指定的JSON jar,并列出其内容(例如使用jar tf),则它不包含org.json.simple 包。

所以问题只是你需要另一个罐子。

编辑:

我不知道这是否是意图,但有根据的猜测:如果您将此依赖项添加到 build.gradle:

compile 'com.googlecode.json-simple:json-simple:1.1.1'

还有这些进口:

import org.json.simple.parser.*;
// import org.json.simple.*;
import org.json.*;

然后示例编译(对我来说)。

【讨论】:

    【解决方案2】:

    将此添加到我的 build.gradle 文件中:

    implementation 'com.googlecode.json-simple:json-simple:1.1.1'
    

    【讨论】:

      【解决方案3】:

      您没有正确的依赖关系来使用org.json.simple 库。

      我认为您可能想要 https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple 之类的依赖项的坐标,但要找到 Maven 坐标并不容易。

      如果您想使用该库,您可以将这些部分添加到您的构建脚本中:

      repositories {
          jcenter()
      }
      
      dependencies {
          compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
      }
      

      将此添加到文件以修复导入:

      import org.json.simple.parser.*;
      

      然后,您只需修复类定义中的使用错误。

      此外,该库看起来无人维护,因此您可能想探索其他 JSON 解析库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 1970-01-01
        • 2014-10-18
        • 2016-05-30
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多