Skip to content

Instantly share code, notes, and snippets.

@TuleSimon
Last active January 8, 2026 20:51
Show Gist options
  • Select an option

  • Save TuleSimon/f2acc015466982c53d3e7c1e10e86ced to your computer and use it in GitHub Desktop.

Select an option

Save TuleSimon/f2acc015466982c53d3e7c1e10e86ced to your computer and use it in GitHub Desktop.
Code Sample
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

Copy link
Copy Markdown
Author
Output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment