23
My problem:
I need to read a JSON that is in a certain URL. I tried the following code, but it doesn’t 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)
Remarks:
- This code is an example and does not include the
try{}catch(...){}
in the example, someone has some notion of how to solve this problem or why it is occurring? - I tried to use
String x = url.toString()
but I also got the same error.
Good question formatting, all well organized :)
– Silvio Andorinha
Look, I know very little about Java, but you shouldn’t pass a string to the parser instead of a Streamreader?
– bfavaretto
Well the Jsonparser class has two parse(String s) and parse(Reader in) methods. As I was reading a local JSON I used Reader, but now the need is another.
– noNihongo