Capture the JSON values that are Online


My problem:

I need to read a JSON that is in a certain URL. I tried the following code, but it does not work:

JSONObject jsonObjeto;
JSONParser parser = new JSONParser();

URL url = new URL("");

String x = url.openStream().toString();
Reader reader = new InputStreamReader(getClass().getResourceAsStream(x));

jsonObjeto = (JSONObject) parser.parse(reader);

I get the following error:

Exception in thread "main" java.lang.NullPointerException


  • This code is an example and does not include try{}catch(...){} in the example, does anyone have any idea how to solve this problem or why it is occurring?
  • I tried to use String x = url.toString() but also got the same error.
asked by anonymous 18.02.2014 / 18:44

4 answers


You already have the stream that is returned by openStream() . Just pass as InputStreamReader argument:

Reader reader = new InputStreamReader( url.openStream() );
18.02.2014 / 19:01

When you call the toString() method on a InputStream , it will not return the contents of the stream, but its memory address. Read a little more about the method toString() here .

You should get the http connection stream and use it to build a Reader and then use JSONParser to parse JSON.

URL url = new URL("");
Reader br = new InputStreamReader(url.openStream());

JSONParser parser = new JSONParser();
JSONObject jsonObjeto = (JSONObject) parser.parse(br);

18.02.2014 / 19:02

What you can use to not have to do everything at hand is:

So, to retrieve data from the URL, just do it:

URL url = new URL( urlString );
return Resources.toString( url, Charsets.UTF_8 );

Another tip is to use: ()

To retrieve the object directly from JSON, like this:

new Gson().fromJson( objetoEmString, ClasseDestinoDaConversao.class );

So, with a little code, everything is ready.

Documentation (in English):



18.02.2014 / 22:43

Good morning.

Just trying to complement the previous responses, at the time of parsing Object - > JSON / JSON - > Subject, I'd rather use Jackson . It supports the JAXB specification. What is good for you to write a code less coupled to the framework.

Anyway. There is an example of the mkyong site by the way is a site that I really like to take examples) that I think can help you. The only difference is that it reads the JSON from a file, but the principle is the same.

You change the line that reads the file by readTree(json) (where json is the JSON String itself)

The code is as follows.

package com.mkyong.core;

import org.codehaus.jackson.JsonGenerationException;

public class JacksonExample {
    public static void main(String[] args) {

    ObjectMapper mapper = new ObjectMapper();

    try {

        // read from file, convert it to user class
        User user = mapper.readValue(new File("c:\user.json"), User.class);

        // display to console

    } catch (JsonGenerationException e) {


    } catch (JsonMappingException e) {


    } catch (IOException e) {





I hope I have helped a little.

26.03.2014 / 14:41