我有这个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);
}
}
感谢您的帮助!
答案 0 :(得分:1)
但是用于反序列化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);
}
}
}
答案 1 :(得分:0)
将变量List<String>
添加到POGO类(GsonCNItem)以解析特征数组。
public class GsonCNItem {
private String context;
private String end;
private List<String> features;
//Setters and Getters
}