1

Well I have been working on parsing a bit of Json. I got this json from a website and stored it locally so I could also test it offline. But now I want to change it and read it directly from the URL (e.g. http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_week.geojson) I thought this would be an easy task but I can't seems to figure it out. This is an example of what Ii have so far.

main class: memberToJava.java

public class memberToJava {

    public static void main(String[] args) throws FileNotFoundException {

        BufferedReader json = new BufferedReader(new FileReader("...Member.json")); 


        memberClass memberClass = new Gson().fromJson(json, memberClass.class);

        System.out.println("Id: " + memberClass.getMember().get(1).getId());  
    }
}

Solution

public class memberToJava {

    public static void main(String[] args) throws FileNotFoundException {

    URL url = new URL(//URL Here);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

    BufferedReader json  = new BufferedReader(new InputStreamReader(connection.getInputStream()));          

        memberClass memberClass = new Gson().fromJson(json, memberClass.class);

        System.out.println("Id: " + memberClass.getMember().get(1).getId());  
    }
}
PHPeter
  • 559
  • 4
  • 17
  • @SotiriosDelimanolis I have no idea how to load the json from an URL – PHPeter Sep 30 '14 at 18:36
  • and you could not find a single lead on that topic? what search engine do you use? – njzk2 Sep 30 '14 at 18:40
  • I used google and the search engine from this website. Maybe I didn't use the right keywords. I searched: gson url java – PHPeter Sep 30 '14 at 18:49
  • This worked for me: `URL url = new URL(//url here"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); BufferedReader json = new BufferedReader(new InputStreamReader(connection.getInputStream()));` – PHPeter Sep 30 '14 at 19:10

0 Answers0