import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
public class ParseJson1 {
public static void main(String[] args) throws IOException {
URL url = new URL("http://freemusicarchive.org/api/get/genres.json?api_key=60BLHNQCAOUFPIBZ&limit=2");
try {
HashMap<String, String> map=new HashMap<String,String>();
String genreJson = IOUtils.toString(url.openStream());
JSONObject genreJsonObject = (JSONObject) JSONValue.parseWithException(genreJson);
Iterator i= genreJsonObject.entrySet().iterator();
while(i.hasNext())
{
Entry ee=(Entry) i.next();
System.out.println(ee.getKey());
System.out.println(ee.getValue());
}
/*
// get the title
System.out.println(genreJsonObject.get("title"));
// get the data
JSONArray genreArray = (JSONArray) genreJsonObject.get("dataset");
// get the first genre
JSONObject firstGenre = (JSONObject) genreArray.get(0);
System.out.println(firstGenre.get("genre_title"));
*/
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
}
Note - Required jars commons-io-1.4.jar , json-simple.jar
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
public class ParseJson1 {
public static void main(String[] args) throws IOException {
URL url = new URL("http://freemusicarchive.org/api/get/genres.json?api_key=60BLHNQCAOUFPIBZ&limit=2");
try {
HashMap<String, String> map=new HashMap<String,String>();
String genreJson = IOUtils.toString(url.openStream());
JSONObject genreJsonObject = (JSONObject) JSONValue.parseWithException(genreJson);
Iterator i= genreJsonObject.entrySet().iterator();
while(i.hasNext())
{
Entry ee=(Entry) i.next();
System.out.println(ee.getKey());
System.out.println(ee.getValue());
}
/*
// get the title
System.out.println(genreJsonObject.get("title"));
// get the data
JSONArray genreArray = (JSONArray) genreJsonObject.get("dataset");
// get the first genre
JSONObject firstGenre = (JSONObject) genreArray.get(0);
System.out.println(firstGenre.get("genre_title"));
*/
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
}
Note - Required jars commons-io-1.4.jar , json-simple.jar