Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:01:49+00:00 2026-06-09T09:01:49+00:00

I’m just starting to mess around with some web programming, and am trying to

  • 0

I’m just starting to mess around with some web programming, and am trying to retrieve information about certain musical artists from a music website’s API. I’m using the JSON.simple toolkit found here.

Here’s my code. Two classes, one is JSONReader:

import org.json.simple.*;
import org.json.simple.parser.*;

import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

public class JSONReader {

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
  }

public static JSONObject readJsonFromUrl(String url) throws IOException, ParseException {
    JSONParser parser = new JSONParser();
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      Object obj = parser.parse(jsonText);
      JSONObject result = (JSONObject) obj;
      return result;

    } finally {
      is.close();
    }
  }

}

Okay, and here’s my Main class:

public class Main {

public static void main(String[] args) throws Exception {
    System.out.println("Starting execution of program...");
    String searchQuery = args[0];
    JSONReader reader = new JSONReader();

    System.out.println("Beginning HTTP request...");
    String baseURL = "http://api.discogs.com/database/search?q=" + searchQuery + "&type=artist";

    JSONObject json = reader.readJsonFromUrl(baseURL);
    System.out.println(json);

The print statement at the end of the main gives me this:

{"results":[{"id":22898,"title":"Tool","type":"artist","resource_url":"http:\/\/api.discogs.com\/artists\/22898","uri":"\/artist\/Tool","thumb":null},{"id":5481,"title":"DJ Tool","type":"artist","resource_url":"http:\/\/api.discogs.com\/artists\/5481","uri":"\/artist\/DJ+Tool","thumb":null},{"id":186087,"title":"Tool (3)","type":"artist","resource_url":"http:\/\/api.discogs.com\/artists\/186087","uri":"\/artist\/Tool+%283%29","thumb":null},{"id":108078,"title":"Rave Tool","type":"artist","resource_url":"http:\/\/api.discogs.com\/artists\

with about a dozen more artists, all distinguished individually by curly braces with commas in-between.

So I “get” the results by doing:

System.out.println(json.get("results"));

And end up with:

[{"id":22898,"title":"Tool","type":"artist","resource_url":"http:\/\/api.discogs.com\/artists\/22898","uri":"\/artist\/Tool","thumb":null},{"id":5481,"title":"DJ Tool", ... ]

The problem is, I want to be able to, say, extract all dozen or so artists from the search result.

I cast the above to a JSONArray as below, and I can then ‘get’ indexes of the search result. My question, after this long post, is: why is this? What, essentially, is the point of the JSONObject in the first place, if it needs to be a JSONArray for accessing index of the results to work? Also, is there a reason I can’t set the result of parsing jsonText directly to a JSONArray? When I try and change all of the “JSONObject”s in the readJsonFromUrl method to JSONArrays, an error occurs, saying that it is unable to cast a JSONObject to a JSONArray. Why is this?

JSONArray json1 = (JSONArray) reader.readJsonFromUrl(baseURL).get("results");
System.out.println(json1);
System.out.println(json1.get(0));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:01:52+00:00Added an answer on June 9, 2026 at 9:01 am

    The reason is simply the structure of your JSON input data: it is a JSON object with only a results property, whose value is an array, this array containing the data you actually want.

    Since you have an object, whose single property is an array with the data that you want, you must parse the whole object to get at the array.

    This seems like a rather trivial object having only a single property, but this isn’t uncommon in JSON responses, indeed in any sort of network communication: the result that you actually want is wrapped in an object that will sometimes contain additional properties that provide meta-information associated with the actual result data.

    Think of this outer object as an envelope that contains your result.

    You can make the result of parsing the response – the result of your readJsonFromUrl method – the array, by discarding the outer object in your read method, but sometimes there will be more to the containing object that you might want to preserve, for instance some header data/meta-information that comes along with the results list, that may influence how you use the data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.