Last active
January 8, 2026 20:51
-
-
Save TuleSimon/f2acc015466982c53d3e7c1e10e86ced to your computer and use it in GitHub Desktop.
Code Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.google.gson.Gson; | |
| import com.google.gson.JsonArray; | |
| import com.google.gson.JsonObject; | |
| import java.util.*; | |
| import java.io.*; | |
| import java.net.*; | |
| class Main { | |
| public static void main(String[] args) { | |
| System.setProperty("http.agent", "Chrome"); | |
| try { | |
| URL url = new URL("https://coderbyte.com/api/challenges/json/rest-get-simple"); | |
| try { | |
| URLConnection connection = url.openConnection(); | |
| String line = ""; | |
| InputStream inputStream = connection.getInputStream(); | |
| BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); | |
| StringBuilder response = new StringBuilder(); | |
| while ((line = bufferedReader.readLine()) != null) { | |
| response.append(line); | |
| } | |
| bufferedReader.close(); | |
| System.out.println("Response: " + response); | |
| Gson gson = new Gson(); | |
| JsonObject jsonObject = gson.fromJson(response.toString(), JsonObject.class); | |
| JsonArray jsonArray = jsonObject.getAsJsonArray("hobbies"); | |
| StringBuilder hobbiesBuilder = new StringBuilder(); | |
| for (int i = 0; i < jsonArray.size(); i++) { | |
| String item = jsonArray.get(i).getAsString(); | |
| hobbiesBuilder.append(item); | |
| if (i != jsonArray.size() - 1) { | |
| hobbiesBuilder.append(", "); | |
| } | |
| } | |
| System.out.println("hobbies: " + hobbiesBuilder); | |
| } catch (IOException ioEx) { | |
| System.out.println(ioEx); | |
| } | |
| } catch (MalformedURLException malEx) { | |
| System.out.println(malEx); | |
| } | |
| } | |
| } |
TuleSimon
commented
Jan 8, 2026
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment