3

I'm trying to pull a JSON document from http://api.conceptnet.io/c/en/concept but I haven't been successful in getting the JSON data into a variable. All I've managed to do is to get the page's source (specifically just the first line, but I understand why I only get one line) with:

InputStream stream = url.openStream();
Scanner scan = new Scanner(stream);
String data = scan.nextLine();
System.out.println(data);

Which isn't helpful. If I could get the JSON data into a String, I can feed that into a JSONObject constructor to build the JSONObject. If I were doing this in python, all I would have to do is:

concept = requests.get('http://api.conceptnet.io/c/en/' + theword).json()

But I can't figure out the equivalent for that in Java. I have very little experience with web requests so I appreciate any help.

3
  • You can use ObjectMapper or JsonObject to parse the response body into a Json. Commented Jun 8, 2020 at 17:59
  • So, your problem is to bring data or to change json string to json object? Commented Jun 8, 2020 at 18:00
  • @KunLun I'm trying to get the data from the website. JSONObject has a constructor that accepts a string to turn into a JSONObject. Commented Jun 8, 2020 at 18:04

2 Answers 2

1

However pythonic way seems much easier, it can't get more easy in Java than this.

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://api.conceptnet.io/c/en/concept")).build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JSONObject myObject = new JSONObject(response.body());
System.out.println(myObject); // Your json object

Don't forget to add the dependencies below.

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;

Dependency for org.json can be found here : https://mvnrepository.com/artifact/org.json/json

Sign up to request clarification or add additional context in comments.

Comments

1

There are multiple options to get a json in java.

  • If you are using Java 11, you can use Java in built web client.

    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("http://api.conceptnet.io/c/en/concept"))
      .build();
    client.sendAsync(request, BodyHandlers.ofString())
      .thenApply(HttpResponse::body)
      .thenAccept(System.out::println)
      .join();

  • Using a library like OkHttp, you have to create a request and feed it to the HttpClient.

    Request request = new Request
       .Builder()
       .url("http://api.conceptnet.io/c/en/concept")
       .get()
       .build()

    OkHttpClient httpClient = client.newBuilder().build()
    Response response = httpClient.newCall(request).execute()
    System.out.println(response.body.string())

4 Comments

Thanks for the help. Do you happen to know where I could download the library for OkHTTP? I can't seem to find any official source for it. Thanks!
OkHttp If you are using a build tool like maven or gradle you can add it as a dependency.
I found the .jar and added it to my build path but when I run a program similar to yours I get a NoClassDefFoundError. I looked that up and an example situation for that was removing a .class file after compilation, which I didn't do. Do you have any idea what's wrong?
Difficult to say without looking at the code and your setup, but it seems like the jar was not loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.