I am building a program using a GET API call to pull artist info in an Eclipse/Java console. I get a good connection to the API that prints out JSON but I'm getting error when trying to pull a specific data point: Cannot invoke "org.json.simple.JSONObject.get(Object)" because "nbalbum" is null
I'm very new to Java and JSON so appreciate pointers here.
Here is my code below with an example JSON return at bottom:
public static void main(String[] args) {
{
}
try {
//scanner for artist name user input in console
Scanner artistscan = new Scanner(System.in); //create scanner
System.out.println("Please enter artist name to search:");
String artistname = artistscan.nextLine(); //turn input into string
System.out.println("Here is how many albums this artist has:");
artistscan.close();
String oldurl = "https://api.deezer.com/search/artist?q="
+artistname+"";
String newurl=oldurl.replaceAll(" ", "");
URL url = new URL(newurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
//Check if connect is made
int responseCode = conn.getResponseCode();
// 200 OK
if (responseCode != 200) {
throw new RuntimeException("HttpResponseCode: " + responseCode);
} else {
StringBuilder informationString = new StringBuilder();
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
informationString.append(scanner.nextLine());
}
//Close the scanner
scanner.close();
//print results
System.out.println(url); //test url is built correctly
System.out.println(informationString);
//Parse JSON results....
// error/returns null:
org.json.simple.parser.JSONParser jsonParser = new JSONParser();
org.json.simple.JSONObject jsonObj = new JSONObject();
jsonObj = (JSONObject) jsonParser.parse(String.valueOf(informationString));
JSONObject nbalbum = (JSONObject) jsonObj.get(0);
System.out.println(nbalbum.get("nb_album"));
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("-------------------------------------");
}}
This is an example JSON return:
{"data":[{"id":566,"name":"Foo Fighters","link":"https:\/\/www.deezer.com\/artist\/566","picture":"https:\/\/api.deezer.com\/artist\/566\/image","picture_small":"https:\/\/e-cdns-images.dzcdn.net\/images\/artist\/54c324b8651addd8c400de22f9dac5c8\/56x56-000000-80-0-0.jpg","picture_medium":"https:\/\/e-cdns-images.dzcdn.net\/images\/artist\/54c324b8651addd8c400de22f9dac5c8\/250x250-000000-80-0-0.jpg","picture_big":"https:\/\/e-cdns-images.dzcdn.net\/images\/artist\/54c324b8651addd8c400de22f9dac5c8\/500x500-000000-80-0-0.jpg","picture_xl":"https:\/\/e-cdns-images.dzcdn.net\/images\/artist\/54c324b8651addd8c400de22f9dac5c8\/1000x1000-000000-80-0-0.jpg","nb_album":37,"nb_fan":4040577,"radio":true,"tracklist":"https:\/\/api.deezer.com\/artist\/566\/top?limit=50","type":"artist"}],"total":1}
Isn't the JSONObject you're getting, have data
field & against this field you have JSON array as value. So if this is true, then maybe fetching json array by jsonObj.get("data")
& then accessing the value at a certain position would probably work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.