16

I'm brand new to Java and have always been a c kind-of-guy. That being said, I'm trying to use the JSON libraries (packages? classes? Java terminology is so damn confusing!) and am having issues adding them as a reference.

These three imports cannot be resolved:

import org.json.simple.JSONArray;

import org.json.simple.JSONObject;

import org.json.simple.JSONValue;

I went to json.org and downloaded the Java libraries but I'm not sure what to do with them. I've tried to go into project properties and add an external class to no avail. I noticed the downloaded folder is full of .java files. What am I supposed to do with these?

Sorry to present such a noob question on here, but I'm stumped.

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Matt
  • 205
  • 1
  • 3
  • 5
  • Think of a `.jar` as a static library in C. When you compile your code you have to explicitly tell `javac` where those jars are. If you're using an IDE there will be a way to add them to your project, otherwise you have to use `-classpath /my/jar/file.jar` with `javac` – Brian Roach Apr 18 '11 at 05:40
  • Why static? JAR is DLL/SO/SL! ;) – Vladimir Dyuzhev Apr 18 '11 at 05:59
  • Yeah ... I was thinking about that after I wrote it but it's late. It's a dynamic library provided you have multiple apps running in the same VM and pulling it from the same classloader ... which would confuse him even more :-D – Brian Roach Apr 18 '11 at 06:09

4 Answers4

18
  • Right click on the Eclipse project,
  • choose Properties
  • Select Java Build Path
  • Click the libraries tab
  • click add external jars
  • find the json jar(s) and add them.
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
15

This is a wrong download I believe. You need JSON-Simple library from here http://code.google.com/p/json-simple/ . Your link points to another implementation.

After download the *.jar should be added to the classpath. How you do it depends on the tools you use. In Eclipse it is right-click on the project, Properties->Libraries and add the new JAR.

Vladimir Dyuzhev
  • 18,130
  • 10
  • 48
  • 62
  • 1
    The jar present on the URL is down now. Any other links available to download the json jar? – Aditya Jan 08 '17 at 15:54
5

Hey just download the json.jar from this website

http://www.java2s.com/Code/Jar/j/Downloadjsonsimple11jar.htm

after that

  • right click on your eclipse project then
  • choose Properties of that folder
  • Select Java Build Path Click the libraries tab
  • click add external jars

find the json jar(s) and add them.

import java.io.FileReader;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

and add these package in your java file :)

Gajender Singh
  • 1,285
  • 14
  • 13
1

Be advised: Json and JsonSimple are not the same. If you are using Maven you can get json-simple by adding this dependency to your pom file:

http://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>
ABC123
  • 1,037
  • 2
  • 20
  • 44