簡體   English   中英

如何使用Jsoup和Gson提取JSON字段

[英]How to extract JSON field using Jsoup and Gson

在我的程序中,我連接到URL以使用Jsoup檢索json

這是用於檢索json的方法[具有subject = example ]:

private void result() throws IOException {
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize,ssplit,pos,lemma,depparse,natlog,openie");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    List<String> list = newsList.getSelectedValuesList();
    for (String s : list) {
       Annotation doc = new Annotation(s);
       pipeline.annotate(doc); 
       for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
            // Get the OpenIE triples for the sentence
            Collection<RelationTriple> triples =
         sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
            // Print the triples
            for (RelationTriple triple : triples) {
                /* replace space with "_" */
                String subject = triple.subjectLemmaGloss().replace(" ", "_");
                String object = triple.objectGloss().replace(" ", "_");
                String kb = "http://api.conceptnet.io/c/en/" + subject;
                Document docKb = Jsoup.connect(kb).get();
                String json = docKb.body().text();
                Gson gson = new Gson();
                Map<String, Object> asMap = gson.fromJson(json.toString(), Map.class);
                List<Map<String, Object>> edges = (List) asMap.get("edges");
                for (Map<String, Object> edge : edges) {
                    if (edge.containsKey("surfaceText") && edge.containsKey("weight")) {
                       txtAreaNews.append(edge.get("surfaceText").toString() + "\n"); 
                       txtAreaNews.append("Weight: " + edge.get("weight").toString());
                    }
                    txtAreaNews.append("\n");
                }
            }
        }
    }
}

這是我從txtAreaNews.append(docKb.text());的URL中檢索到的html txtAreaNews.append(docKb.text());

 ConceptNet 5 API {
  "@context": [
    "http://api.conceptnet.io/ld/conceptnet5.5/context.ld.json",
    "http://api.conceptnet.io/ld/conceptnet5.5/pagination.ld.json"
  ],
  "@id": "/c/en/example",
  "edges": [
    {
      "@id": "/a/[/r/IsA/,/c/en/apology/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/IsA",
        "label": "IsA"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/apology/n",
        "label": "apology",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/apology"
      },
      "surfaceText": "[[apology]] is a type of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/IsA/,/c/en/example/n/,/c/en/admonition/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/admonition/n",
        "label": "admonition",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/admonition"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/IsA",
        "label": "IsA"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "surfaceText": "[[example]] is a type of [[admonition]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/IsA/,/c/en/example/n/,/c/en/information/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/information/n",
        "label": "information",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/information"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/IsA",
        "label": "IsA"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "surfaceText": "[[example]] is a type of [[information]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/IsA/,/c/en/exception/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/IsA",
        "label": "IsA"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/exception/n",
        "label": "exception",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/exception"
      },
      "surfaceText": "[[exception]] is a type of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/IsA/,/c/en/precedent/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/IsA",
        "label": "IsA"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/precedent/n",
        "label": "precedent",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/precedent"
      },
      "surfaceText": "[[precedent]] is a type of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/IsA/,/c/en/quintessence/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/IsA",
        "label": "IsA"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/quintessence/n",
        "label": "quintessence",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/quintessence"
      },
      "surfaceText": "[[quintessence]] is a type of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/IsA/,/c/en/sample/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/IsA",
        "label": "IsA"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/sample/n",
        "label": "sample",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/sample"
      },
      "surfaceText": "[[sample]] is a type of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/IsA/,/c/en/specimen/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/IsA",
        "label": "IsA"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/specimen/n",
        "label": "specimen",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/specimen"
      },
      "surfaceText": "[[specimen]] is a type of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/RelatedTo/,/c/en/exemplary/a/,/c/en/example/]",
      "dataset": "/d/wiktionary/en",
      "end": {
        "@id": "/c/en/example",
        "label": "example",
        "language": "en",
        "term": "/c/en/example"
      },
      "license": "cc:by-sa/4.0",
      "rel": {
        "@id": "/r/RelatedTo",
        "label": "RelatedTo"
      },
      "sources": [
        {
          "@id": "/and/[/s/process/wikiparsec/1/,/s/resource/wiktionary/en/]",
          "contributor": "/s/resource/wiktionary/en",
          "process": "/s/process/wikiparsec/1"
        },
        {
          "@id": "/and/[/s/process/wikiparsec/1/,/s/resource/wiktionary/fr/]",
          "contributor": "/s/resource/wiktionary/fr",
          "process": "/s/process/wikiparsec/1"
        }
      ],
      "start": {
        "@id": "/c/en/exemplary/a",
        "label": "exemplary",
        "language": "en",
        "sense_label": "a",
        "term": "/c/en/exemplary"
      },
      "surfaceText": null,
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/RelatedTo/,/c/en/exemplify/v/,/c/en/example/]",
      "dataset": "/d/wiktionary/en",
      "end": {
        "@id": "/c/en/example",
        "label": "example",
        "language": "en",
        "term": "/c/en/example"
      },
      "license": "cc:by-sa/4.0",
      "rel": {
        "@id": "/r/RelatedTo",
        "label": "RelatedTo"
      },
      "sources": [
        {
          "@id": "/and/[/s/process/wikiparsec/1/,/s/resource/wiktionary/en/]",
          "contributor": "/s/resource/wiktionary/en",
          "process": "/s/process/wikiparsec/1"
        },
        {
          "@id": "/and/[/s/process/wikiparsec/1/,/s/resource/wiktionary/fr/]",
          "contributor": "/s/resource/wiktionary/fr",
          "process": "/s/process/wikiparsec/1"
        }
      ],
      "start": {
        "@id": "/c/en/exemplify/v",
        "label": "exemplify",
        "language": "en",
        "sense_label": "v",
        "term": "/c/en/exemplify"
      },
      "surfaceText": null,
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/ar/مِثَال/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/ar/مِثَال/n",
        "label": "مِثَال",
        "language": "ar",
        "sense_label": "n",
        "term": "/c/ar/مِثَال"
      },
      "surfaceText": "[[مِثَال]] is a translation of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/ar/مَثَل/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/ar/مَثَل/n",
        "label": "مَثَل",
        "language": "ar",
        "sense_label": "n",
        "term": "/c/ar/مَثَل"
      },
      "surfaceText": "[[مَثَل]] is a translation of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/ca/escarment/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/ca/escarment/n",
        "label": "escarment",
        "language": "ca",
        "sense_label": "n",
        "term": "/c/ca/escarment"
      },
      "surfaceText": "[[escarment]] is a translation of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/ca/exemple/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/ca/exemple/n",
        "label": "exemple",
        "language": "ca",
        "sense_label": "n",
        "term": "/c/ca/exemple"
      },
      "surfaceText": "[[exemple]] is a translation of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/ca/lliçó/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/ca/lliçó/n",
        "label": "lliçó",
        "language": "ca",
        "sense_label": "n",
        "term": "/c/ca/lliçó"
      },
      "surfaceText": "[[lliçó]] is a translation of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/da/eksempel/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/da/eksempel/n",
        "label": "eksempel",
        "language": "da",
        "sense_label": "n",
        "term": "/c/da/eksempel"
      },
      "surfaceText": "[[eksempel]] is a translation of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/en/deterrent_example/n/,/c/en/example/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/deterrent_example/n",
        "label": "deterrent example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/deterrent_example"
      },
      "surfaceText": "[[deterrent example]] is a synonym of [[example]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/en/example/n/,/c/en/case/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/case/n",
        "label": "case",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/case"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "surfaceText": "[[example]] is a synonym of [[case]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/en/example/n/,/c/en/exemplar/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/exemplar/n",
        "label": "exemplar",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/exemplar"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "surfaceText": "[[example]] is a synonym of [[exemplar]]",
      "weight": 2.0
    },
    {
      "@id": "/a/[/r/Synonym/,/c/en/example/n/,/c/en/exercise/n/]",
      "dataset": "/d/wordnet/3.1",
      "end": {
        "@id": "/c/en/exercise/n",
        "label": "exercise",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/exercise"
      },
      "license": "cc:by/4.0",
      "rel": {
        "@id": "/r/Synonym",
        "label": "Synonym"
      },
      "sources": [
        {
          "@id": "/s/resource/wordnet/rdf/3.1",
          "contributor": "/s/resource/wordnet/rdf/3.1"
        }
      ],
      "start": {
        "@id": "/c/en/example/n",
        "label": "example",
        "language": "en",
        "sense_label": "n",
        "term": "/c/en/example"
      },
      "surfaceText": "[[example]] is a synonym of [[exercise]]",
      "weight": 2.0
    }
  ],
  "view": {
    "@id": "/c/en/example?offset=0&limit=20",
    "firstPage": "/c/en/example?offset=0&limit=20",
    "nextPage": "/c/en/example?offset=20&limit=20",
    "paginatedProperty": "edges"
  }
}

null的surfaceText示例:

{
   "@id": "/a/[/r/RelatedTo/,/c/en/exemplary/a/,/c/en/example/]",
   "dataset": "/d/wiktionary/en",
   "end": {
     "@id": "/c/en/example",
     "label": "example",
     "language": "en",
     "term": "/c/en/example"
   },
   "license": "cc:by-sa/4.0",
  "rel": {
     "@id": "/r/RelatedTo",
     "label": "RelatedTo"
   },
   "sources": [
     {
       "@id": "/and/[/s/process/wikiparsec/1/,/s/resource/wiktionary/en/]",
       "contributor": "/s/resource/wiktionary/en",
       "process": "/s/process/wikiparsec/1"
     },
     {
       "@id": "/and/[/s/process/wikiparsec/1/,/s/resource/wiktionary/fr/]",
       "contributor": "/s/resource/wiktionary/fr",
       "process": "/s/process/wikiparsec/1"
     }
   ],
   "start": {
     "@id": "/c/en/exemplary/a",
     "label": "exemplary",
     "language": "en",
     "sense_label": "a",
     "term": "/c/en/exemplary"
   },
   "surfaceText": null,
   "weight": 2.0
}

我想問一下,如何使用Jsoup或Gson從上面獲取的數據中獲取"surfaceText""weight"

您可以使用Gson將JSON反序列化為Map ,然后查詢Map

這是一個例子:

String kb = "http://api.conceptnet.io/c/en/example";
Document docKb = Jsoup.connect(kb).get();
String json = docKb.body().text();

Gson gson = new Gson();
Map<String, Object> asMap = gson.fromJson(json, Map.class);
List<Map<String, Object>> edges = (List) asMap.get("edges");
for (Map<String, Object> edge : edges) {
    // check that the response contains a surfaceText
    if (edge.containsKey("surfaceText")) {
        // check that the surfaceText value is non-null
        String surfaceText = (String) edge.get("surfaceText");
        if (surfaceText != null) {
            System.out.println(edge.get("surfaceText"));
        }
    }
    if (edge.containsKey("weight")) {
        System.out.println(edge.get("weight"));
    }
}

該代碼將打印出來:

[[apology]] is a type of [[example]]
2.0
[[example]] is a type of [[admonition]]
2.0
[[example]] is a type of [[information]]
2.0
[[exception]] is a type of [[example]]
2.0
... etc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM