0

I use Eclipse.I am reading a url and retrieving a json string:

String message = "http://XXXXXXXXX";

try {
            website = new URL(message);
            URLConnection connection = website.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));


            String inputLine;
            while ((inputLine = in.readLine()) != null) 
                str1 += inputLine;



        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

try {
                JSONObject jsono = new JSONObject(str1);
                JSONObject results =jsono.getJSONObject("results");
                JSONArray result =results.getJSONArray("result");



            } catch(JSONException ex) {
                System.out.println("Exception thrown: " + ex.getMessage());
            }

And I can get it, I can print it out. Then I wanna parse the json string using JSONArray and JSONObject, but there are errors in the three lines:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

The error is : The import org.json cannot be resolved.

I am stuck here.

user23256
  • 53
  • 2
  • 9

2 Answers2

0

You need to compile and run against the appropriate jar file.

The above imports merely declare the usage of those classes. You need to provide them during compilation and then during the running of your program e.g.

javac -cp json.jar MyTest.java

then

java -cp json.jar;. MyTest

(for example - your compilation and/or packaging may be different)

This SO question has more detailed answers.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

You need to include Json jar in your library .

Download the ZIP file from here and extract it to get the Jar. Add the Jar to your build path. To check the available classes in this Jar use this URL.

To Add this Jar to your build path

Right click the Project > Build Path > Configure build path> Select Libraries tab > Click Add External Libraries > Select the Jar file Download

I hope this will solve your problem.

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
The Dark Knight
  • 5,455
  • 11
  • 54
  • 95