简体   繁体   中英

Deciphering JSON from AcousticBrainz

I stumbled upon AcousticBrainz and I am trying to get the data from their database. They give me the API (acousticbrainz.org/api). Unfortunatley I am new to JSON and I am not exactly sure how to get the info. I am trying to get the info from http://acousticbrainz.org/8e160042-b451-4f96-b59a-d06831d2ae05/high-level

I know that there are only two JSON Objects. "highlevel" and "metadata"

This is how they present their info http://acousticbrainz.org/8e160042-b451-4f96-b59a-d06831d2ae05

I am coding in Java and this is what I have

String URL = "http://acousticbrainz.org/8e160042-b451-4f96-b59a-d06831d2ae05/high-level";
String Json = IOUtils.toString(new URL(URL));
JSONObject jo = new JSONObject(Json);
JSONObject jom = new JSONObject(jo.get("metadata"));

System.out.println(jo.names());
System.out.println(jom.names());
System.out.println(jo.get("metadata"));

If you are looking to make API call and get the json data you can use the following code:

URIBuilder builder = new URIBuilder().setScheme("http")
        .setHost("acousticbrainz.org")
        .setPath("/8e160042-b451-4f96-b59a-d06831d2ae05/high-level");

HttpUriRequest httpUriRequest = new HttpGet(builder.build());

HttpResponse httpResponse = HttpClientBuilder.create().build().execute(httpUriRequest);

String response = EntityUtils.toString(httpResponse.getEntity());

JSONObject jsonObject = new JSONObject(response);

System.out.println(jsonObject.toString());

And if you want to parse the json data and use it you can check the below sample code:

JSONObject metaDataObject = jsonObject.getJSONObject("metadata");

String md5EncodedValue = metaDataObject.getJSONObject("audio_properties")
        .getString("md5_encoded");

System.out.println(md5EncodedValue);

if you are using maven then add the following dependency

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20131018</version>
</dependency>

or use this link to download the jar

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM