Capture the JSON values that are Online

22

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("http://www.exemplo.br/teste.json");

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
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)

Notes:

  • 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

7

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
11

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("http://www.exemplo.br/teste.json");
Reader br = new InputStreamReader(url.openStream());

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

System.out.println(jsonObjeto);
    
18.02.2014 / 19:02
6

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

com.google.common.io.Resources.

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:

com.google.gson.Gson.Gson ()

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):

link

link

    
18.02.2014 / 22:43
3

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 java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

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
        System.out.println(user);

    } catch (JsonGenerationException e) {

        e.printStackTrace();

    } catch (JsonMappingException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

  }

}

I hope I have helped a little.

    
26.03.2014 / 14:41