Gradle dependency json-simple error

心已入冬 提交于 2019-12-10 14:53:23

问题


I'm fairly new to Gradle so I'm trying to build a Java project and not sure about the dependencies. I have never gotten Gradle configured to be able to do my tests or now a jar file to compile and run.

My 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'
}

And this is what I get on the console stating that it doesn't see my import:

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

Here's my class:

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();
    }
  }
}

回答1:


If you download the JSON jar specified, and list its contents (e.g. with jar tf), it does not contain the org.json.simple package.

So the problem is simply that you need another jar.

EDIT:

I don't know if this is the intent, but an educated guess: if you add this dependency to build.gradle:

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

and these imports:

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

then the example compiles (for me).




回答2:


Adding this to my build.gradle file works:

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



回答3:


You don't have the correct dependency to use org.json.simple libraries.

I think you may want the coordinates for a dependency like https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple , but it is not easy to find the Maven coordinates.

If you wanted to use that library you could add these parts to your build script:

repositories {
    jcenter()
}

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

Add this to the file to fix imports:

import org.json.simple.parser.*;

Then, you just have to fix the usage errors in your class definition.

Also, that library looks unmaintained so you may want to explore other JSON parsing libraries.



来源:https://stackoverflow.com/questions/46958715/gradle-dependency-json-simple-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!