gpt4 book ai didi

java - 如何用Gson提取字段?

转载 作者:行者123 更新时间:2023-12-01 13:19:20 33 4
gpt4 key购买 nike

我有这个 json 代码(它是整个 json 代码的一部分):

{
"numFound": 41,
"edges": [
{
"endLemmas": "albero",
"context": "/ctx/all",
"end": "/c/it/albero/v",
"features": [
"/c/it/alberatura /r/ConceptuallyRelatedTo -",
"/c/it/alberatura - /c/it/albero/v",
"- /r/ConceptuallyRelatedTo /c/it/albero/v"
],
"license": "/l/CC/By-SA",
"start": "/c/it/alberatura",
"startLemmas": "alberatura",
"text": [
"albero",
"alberatura"
],
"uri": "/a/[/r/ConceptuallyRelatedTo/,/c/it/alberatura/,/c/it/albero/v/]",
"weight": 1.0,
"dataset": "/d/conceptnet/5/combined-sa",
"sources": [
"/and/[/s/rule/wiktionary_monolingual_definitions/,/s/web/en.wiktionary.org/]"
],
"score": 7.81056,
"rel": "/r/ConceptuallyRelatedTo",
"timestamp": "2013-09-12T16:30:04.157Z",
"nodes": [
"/r/ConceptuallyRelatedTo",
"/c/it/albero/v",
"/c/it/alberatura"
],
"id": "/e/021aa72c7605d9d2000f0774a0bf9042699fa6ee",
"surfaceText": "[[alberatura]] ConceptuallyRelatedTo [[albero]]"
},

如何提取“特征”字段?我可以提取一些字段,例如“上下文”或“结束”,但对于“特征”我不能! :(

我的Java代码:

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

URL url = new URL("http://conceptnet5.media.mit.edu/data/5.1/c/it/albero?get=context");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

String inputLine;

GsonCNItem gsonCNObj = new Gson().fromJson(br, GsonCNItem.class);

for (int i = 0; i < 3; i++) {
try{
gsonCNObj.getCNThing(i);
}catch (IndexOutOfBoundsException e) {}
}
}

我的 GsonCNItem 类:

public class GsonCNItem {

private String context;
private String end;
private List<GsonCNItem> edges;

public List<GsonCNItem> getCNedges(){
return edges;
}

public void getCNThing (int i){
System.out.println(edges.get(i));
}

public String toString(){
return String.format("Context: %s\nend: %s",context,end);
}
}

感谢您的帮助!

最佳答案

但是你的反序列化 JSON 的类是错误的,它应该如下所示:

public class MyJson {
private long numFound;
private ArrayList<GsonCNItem> edges;

public ArrayList<GsonCNItem> getEdges(){
return getEdges();
}

public class GsonCNItem{
private String endLemmas;
private String context;
private String end;
private ArrayList<String> features;
...
private ArrayList<String> text;
...

public ArrayList<String> getFeatures(){
return features;
}

public ArrayList<String> getText(){
return text;
}

public String toString(){
return String.format("Context: %s\nend: %s",context,end);
}
}
}

关于java - 如何用Gson提取字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22172607/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com