java - ConceptNet 数据库与 Java 的连接

标签 java json csv conceptnet

有人知道如何用 Java 连接 ConceptNet 数据库吗?我搜索了不同的教程,检查了不同的论坛,但我仍然找不到正确的方法。

此外,如何使用 Java 向 ConceptNet 获取数据或从 ConceptNet 发布数据。

有人告诉我,通过使用 JSON 或 Flat Csv,我可以实现查询的回复,但我不熟悉这两种技术,也不熟悉如何将它们与 ConceptNet 数据库和 Java 一起使用。

如果有人知道请回复我...

最佳答案

这是我从 ConceptNet 访问查询所做的操作。我使用了 org.apache.commons.io.IOUtils 和 org.json maven 目录。希望这会有所帮助。

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.json.*;

public class httprequestpractice {
    public static void main(String[] args) {
        try {
            // url containing the word to be indexed
            String obj = "http://api.conceptnet.io/c/en/example";
            // open HttpURLConnection
            HttpURLConnection hp = (HttpURLConnection) new URL(obj)
                    .openConnection();
            // set to request method to get
            // not required since default
            hp.setRequestMethod("GET");
            // get the inputstream in the json format
            hp.setRequestProperty("Accept", "application/json");
            // get inputstream from httpurlconnection
            InputStream is = hp.getInputStream();
            // get text from inputstream using IOUtils
            String jsonText = IOUtils.toString(is, Charset.forName("UTF-8"));
            // get json object from the json String
            JSONObject json = new JSONObject(jsonText);
            // get the edges array from the JSONObject which contains all
            // content
            JSONArray edges = json.getJSONArray("edges");
            // goes through the edges array
            for (int x = 0; x < edges.length(); x++) {
                // convert the first object of the json array into a jsonobject
                // once it is a jsonobject you can use getString or getJSONArray
                // to continue in getting info
                System.out.println(
                        edges.getJSONObject(x));
            }
            is.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

关于java - ConceptNet 数据库与 Java 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295253/

相关文章:

javascript - 有没有一种方便的方法可以在不转换的情况下从 CSV 文件将值打印到 JS 图表?

PHP 将索引数组导出到 CSV

Java 随机 ListArray 上的冒泡排序

java - 修复我的Java类中的数组类型和方法错误,在解决该错误时确实迷失了方向

java - 使用 Blobstore 将大文件上传到 Google App Engine

php - 我可以将 PHP 变量插入 JSON 字符串吗?

json - 在单个类中处理相同值的 2 个键

Delphi 中的 JSON 数组

php - 使用 PHP 进行 CSV 到 MySQL 的 500 内部服务器错误

java - 如何提取两个分隔符之间的字符串