user3007875
user3007875

Reputation: 153

Using JSON in Eclipse

I am a novice in using JSON, so my problem should be easy for you. Basically I added the JSON jar as external JAR, and tried to test it. I can make object but any method on it do not work. Here is screenshot: http://shrani.si/f/1l/GU/4elwJxP4/zajeta-slika.jpg

What do I need to do to make JSON code work?

Upvotes: 0

Views: 502

Answers (1)

Scrooge McD
Scrooge McD

Reputation: 335

In Eclipse , navigate to

Windows ->Preferences -> Java -> Build Path -> ClassPath Variables

If your classpath variable 'JSON_JARS' is not present there, add it there, alongwith its location.

This should resolve your 'unbound classpath variable' error, if you have this error.

Moreover, put your execution codes in some method, or at least in main. For example :

package test;

import org.json.simple.*;

public class JSONTest {

    public void testMethod(){
        JSONObject obj = new JSONObject();
        obj.put("name","test");

        JSONArray list = new JSONArray();
        list.add("1234");
    }

    public static void main(String [] args){
        JSONObject obj = new JSONObject();
        obj.put("name","test");

        JSONArray list = new JSONArray();
        list.add("1234");
    }
}

This doesn't throw any error.

For the code which you have attached, if I am correct, there is no way for the code to populate the object and list, to get executed. This is why you get those errors.

Upvotes: 1

Related Questions