Gradle依赖json-simple错误

Apr*_*ara 5 junit dependencies gradle json-simple build.gradle

我是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'
}
Run Code Online (Sandbox Code Playgroud)

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

 error: package org.json.simple does not exist
 import org.json.simple.JSONParser;
Run Code Online (Sandbox Code Playgroud)

这是我的班级:

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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*ter 10

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

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

编辑:

我不知道这是不是意图,而是一个有根据的猜测:如果你将这个依赖添加到build.gradle:

compile 'com.googlecode.json-simple:json-simple:1.1.1'
Run Code Online (Sandbox Code Playgroud)

和这些进口:

import org.json.simple.parser.*;
// import org.json.simple.*;
import org.json.*;
Run Code Online (Sandbox Code Playgroud)

然后这个例子编译(对我来说).


小智 6

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

implementation 'com.googlecode.json-simple:json-simple:1.1.1'
Run Code Online (Sandbox Code Playgroud)