繁体   English   中英

如何获得纯 JSON 字符串而不是 HTML

[英]How do I get a pure JSON string instead of HTML

尝试从 JAVA 中的 URL 获取 JSON-LD 数据。 但结果显示在 HTML 中。

就在 JSON 数据开始之前,它会显示此消息。

<!doctype html><!-- This is an HTML rendering of the ConceptNet API response. We're showing you HTML so you get convenient syntax highlighting and links, because we think you're seeing this in a Web browser, based on your 'Accept: text/html' header. If you're using a client that isn't a Web browser, you probably wanted pure JSON. In that case, set the header 'Accept: */*' or 'Accept: application/json' on your request, or add the '?format=json' parameter.

如何设置适当的标头以获取纯 JSON 格式而不是 HTML 格式的字符串。

代码是

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.http.HttpResponse;
import java.util.Scanner;
public class test{
    public static void main(String[] args) throws IOException {
        URL url=new URL("http://api.conceptnet.io/c/en/french_toast");
        HttpURLConnection conn=(HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        conn.connect();

        int responsecode=conn.getResponseCode();
        System.out.println(responsecode);
        if(responsecode!=200){
            throw new RuntimeException("HttpResponseCode:"+responsecode);

        }
        else{
            Scanner sc=new Scanner(url.openStream());
            String inline="";
            while(sc.hasNext()){
                inline+=sc.nextLine();

            }
            System.out.println("\nJson data in String format");
            System.out.println(inline);
            sc.close();


        }


    }


} 

好的,由于您使用的是 HttpURLConnection,因此您可以使用一种方法来设置标头,就像这样。

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Accept", "application/json");

使用 HTML 解析器,例如jsoup

import java.io.IOException;
import java.net.URL;

import org.jsoup.Jsoup;

public class Main {
    public static void main(String args[]) throws IOException {
        System.out.println(Jsoup.parse(new URL("http://api.conceptnet.io/c/en/french_toast"), 10000).body().text());
    }
}

输出:

{
  "@context": [
    "http://api.conceptnet.io/ld/conceptnet5.7/context.ld.json"
  ],
  "@id": "/c/en/french_toast",
  "edges": [
    {
...
...
...

请在您的 URL 末尾添加?format=json ,无需更多操作。

喜欢:

 URL url=new URL("http://api.conceptnet.io/c/en/french_toast?format=json");

您的代码:

public static void main(String[] args) throws IOException {

        //Only this line (Url) has been edited (?format=json added)
        URL url = new URL("http://api.conceptnet.io/c/en/french_toast?format=json");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.connect();

        int responsecode = conn.getResponseCode();
        System.out.println(responsecode);
        if (responsecode != 200) {
            throw new RuntimeException("HttpResponseCode:" + responsecode);

        } else {
            Scanner sc = new Scanner(url.openStream());
            String inline = "";
            while (sc.hasNext()) {
                inline += sc.nextLine();

            }
            System.out.println("\nJson data in String format");
            System.out.println(inline);
            sc.close();

        }

    }

结果 :

Json data in String format
{"@context": ["http://api.conceptnet.io/ld/conceptnet5.7/context.ld.json"], "@id": "/c/en/french_toast", "edges": [{"@id": "/a/[/r/Synonym/,/c/fr/pain_perdu/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/fr/pain_perdu/n/wn/food", "@type": "Node", "label": "Pain perdu", "language": "fr", "sense_label": "n, food", "term": "/c/fr/pain_perdu"}, "surfaceText": "[[Pain perdu]] is a translation of [[French toast]]", "weight": 3.4641016151377544}, {"@id": "/a/[/r/Synonym/,/c/pt/rabanada/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/pt/rabanada/n/wn/food", "@type": "Node", "label": "rabanada", "language": "pt", "sense_label": "n, food", "term": "/c/pt/rabanada"}, "surfaceText": "[[rabanada]] is a translation of [[French toast]]", "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/ja/フレンチトースト/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/ja/フレンチトースト/n/wn/food", "@type": "Node", "label": "フレンチトースト", "language": "ja", "sense_label": "n, food", "term": "/c/ja/フレンチトースト"}, "surfaceText": "[[フレンチトースト]] is a translation of [[French toast]]", "weight": 2.0}, {"@id": "/a/[/r/RelatedTo/,/c/en/french_toast/n/,/c/fr/pain_perdu/]", "@type": "Edge", "dataset": "/d/wiktionary/fr", "end": {"@id": "/c/fr/pain_perdu", "@type": "Node", "label": "pain perdu", "language": "fr", "term": "/c/fr/pain_perdu"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/RelatedTo", "@type": "Relation", "label": "RelatedTo"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/fr/]", "@type": "Source", "contributor": "/s/resource/wiktionary/fr", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/th/เฟรนช์โทสต์/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/th/เฟรนช์โทสต์/n/wn/food", "@type": "Node", "label": "เฟรนช์โทสต์", "language": "th", "sense_label": "n, food", "term": "/c/th/เฟรนช์โทสต์"}, "surfaceText": "[[เฟรนช์โทสต์]] is a translation of [[French toast]]", "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/ja/フレンチ_トースト/n/,/c/en/french_toast/]", "@type": "Edge", "dataset": "/d/jmdict", "end": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/jmdict/1.07", "@type": "Source", "contributor": "/s/resource/jmdict/1.07"}], "start": {"@id": "/c/ja/フレンチ_トースト/n", "@type": "Node", "label": "フレンチ トースト", "language": "ja", "sense_label": "n", "term": "/c/ja/フレンチ_トースト"}, "surfaceText": null, "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/fr/pain_doré/n/wn/food/,/c/en/french_toast/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "license": "cc:by/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/fr/pain_doré/n/wn/food", "@type": "Node", "label": "pain doré", "language": "fr", "sense_label": "n, food", "term": "/c/fr/pain_doré"}, "surfaceText": "[[pain doré]] is a translation of [[French toast]]", "weight": 2.0}, {"@id": "/a/[/r/IsA/,/c/en/french_toast/n/wn/food/,/c/en/dish/n/wn/food/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "/c/en/dish/n/wn/food", "@type": "Node", "label": "dish", "language": "en", "sense_label": "n, food", "term": "/c/en/dish"}, "license": "cc:by/4.0", "rel": {"@id": "/r/IsA", "@type": "Relation", "label": "IsA"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "French toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "surfaceText": "[[French toast]] is a type of [[dish]]", "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/sh/prženica/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/sh/prženica", "@type": "Node", "label": "prženica", "language": "sh", "term": "/c/sh/prženica"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/ja/フレンチトースト/n/,/c/en/french_toast/]", "@type": "Edge", "dataset": "/d/jmdict", "end": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/s/resource/jmdict/1.07", "@type": "Source", "contributor": "/s/resource/jmdict/1.07"}], "start": {"@id": "/c/ja/フレンチトースト/n", "@type": "Node", "label": "フレンチトースト", "language": "ja", "sense_label": "n", "term": "/c/ja/フレンチトースト"}, "surfaceText": null, "weight": 2.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/zh/西多/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/zh/西多", "@type": "Node", "label": "西多", "language": "zh", "term": "/c/zh/西多"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/HasContext/,/c/en/french_toast/n/,/c/en/culinary/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/en/culinary", "@type": "Node", "label": "culinary", "language": "en", "term": "/c/en/culinary"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/HasContext", "@type": "Relation", "label": "HasContext"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/ExternalURL/,/c/en/french_toast/n/wn/food/,/http://wordnet-rdf.princeton.edu/wn31/107884176-n/]", "@type": "Edge", "dataset": "/d/wordnet/3.1", "end": {"@id": "http://wordnet-rdf.princeton.edu/wn31/107884176-n", "@type": "Node", "label": "107884176-n", "site": "wordnet-rdf.princeton.edu", "site_available": true, "term": "http://wordnet-rdf.princeton.edu/wn31/107884176-n"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/ExternalURL", "@type": "Relation", "label": "ExternalURL"}, "sources": [{"@id": "/s/resource/wordnet/rdf/3.1", "@type": "Source", "contributor": "/s/resource/wordnet/rdf/3.1"}], "start": {"@id": "/c/en/french_toast/n/wn/food", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n, food", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/Synonym/,/c/fr/pain_perdu/n/,/c/en/french_toast/]", "@type": "Edge", "dataset": "/d/wiktionary/fr", "end": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/fr/]", "@type": "Source", "contributor": "/s/resource/wiktionary/fr", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/fr/pain_perdu/n", "@type": "Node", "label": "pain perdu", "language": "fr", "sense_label": "n", "term": "/c/fr/pain_perdu"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/nl/wentelteefje/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/nl/wentelteefje", "@type": "Node", "label": "wentelteefje", "language": "nl", "term": "/c/nl/wentelteefje"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/ExternalURL/,/c/en/french_toast/,/http://wikidata.dbpedia.org/resource/Q197973/]", "@type": "Edge", "dataset": "/d/dbpedia/en", "end": {"@id": "http://wikidata.dbpedia.org/resource/Q197973", "@type": "Node", "label": "Q197973", "site": "wikidata.dbpedia.org", "site_available": true, "term": "http://wikidata.dbpedia.org/resource/Q197973"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/ExternalURL", "@type": "Relation", "label": "ExternalURL"}, "sources": [{"@id": "/s/resource/dbpedia/2015/en", "@type": "Source", "contributor": "/s/resource/dbpedia/2015/en"}], "start": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/DerivedFrom/,/c/en/french_toast/,/c/en/french/a/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/en/french/a", "@type": "Node", "label": "french", "language": "en", "sense_label": "a", "term": "/c/en/french"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/DerivedFrom", "@type": "Relation", "label": "DerivedFrom"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/ms/roti_bakar/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/ms/roti_bakar", "@type": "Node", "label": "roti bakar", "language": "ms", "term": "/c/ms/roti_bakar"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/ExternalURL/,/c/en/french_toast/,/http://dbpedia.org/resource/French_toast/]", "@type": "Edge", "dataset": "/d/dbpedia/en", "end": {"@id": "http://dbpedia.org/resource/French_toast", "@type": "Node", "label": "French_toast", "site": "dbpedia.org", "site_available": true, "term": "http://dbpedia.org/resource/French_toast"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/ExternalURL", "@type": "Relation", "label": "ExternalURL"}, "sources": [{"@id": "/s/resource/dbpedia/2015/en", "@type": "Source", "contributor": "/s/resource/dbpedia/2015/en"}], "start": {"@id": "/c/en/french_toast", "@type": "Node", "label": "french toast", "language": "en", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}, {"@id": "/a/[/r/Synonym/,/c/en/french_toast/n/,/c/zh/法兰西吐司/]", "@type": "Edge", "dataset": "/d/wiktionary/en", "end": {"@id": "/c/zh/法兰西吐司", "@type": "Node", "label": "法兰西吐司", "language": "zh", "term": "/c/zh/法兰西吐司"}, "license": "cc:by-sa/4.0", "rel": {"@id": "/r/Synonym", "@type": "Relation", "label": "Synonym"}, "sources": [{"@id": "/and/[/s/process/wikiparsec/2/,/s/resource/wiktionary/en/]", "@type": "Source", "contributor": "/s/resource/wiktionary/en", "process": "/s/process/wikiparsec/2"}], "start": {"@id": "/c/en/french_toast/n", "@type": "Node", "label": "french toast", "language": "en", "sense_label": "n", "term": "/c/en/french_toast"}, "surfaceText": null, "weight": 1.0}], "view": {"@id": "/c/en/french_toast?offset=0&limit=20", "@type": "PartialCollectionView", "comment": "There are more results. Follow the 'nextPage' link for more.", "firstPage": "/c/en/french_toast?offset=0&limit=20", "nextPage": "/c/en/french_toast?offset=20&limit=20", "paginatedProperty": "edges"}}

使用 postman 或浏览器 rest api-plugin 之类的工具尝试相同的请求。

您可以使用这些工具修改您的请求标头,而浏览器则无法做到这一点。 将请求标头内容类型设置为 application/json 可以解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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