使用Gson反序列化一个对象数组

时间:2011-05-11 05:22:32

标签: java json gson

我有一个看起来像这样的JSON响应。我想提取“text”的值并将它们放入一组字符串中(即我不一定需要导出整个JSON)。

我正在使用GSON

到目前为止,我的方法看起来像这样(显然是错误的):

 public static Response deserialise(String json){
  Gson gson = new Gson();
  Response r = gson.fromJson(json, Response.class);
  return r;
 }

我打电话给deserialise

Response r = deserialise(json);
System.out.println("[status]: "+r.getStatus());  // works fine
        Collection<Keyword> coll = r.getKeywords();
        Iterator<Keyword> itr = coll.iterator();
        while(itr.hasNext()){
System.out.println(itr.next().getWord());        //prints null every time
 }

Response是一个包含以下成员变量的类(带有getter和setter):

private String status;
private String usage;
private String language;
private Collection<Keyword> keywords; 

关键字是一个包含以下成员变量的类(带有getter和setter):

private String word;
private String relevance;

JSON看起来像这样:

{
"status": "OK",
"usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html",
"url": "http://www.theage.com.au/world/aussie-trying-to-make-a-difference-gunned-down-20110510-1egnv.html",
"language": "english",
"keywords": [
    {
        "text": "Mr McNichols",
        "relevance": "0.99441"
    },
    {
        "text": "Ms Benton",
        "relevance": "0.392337"
    },
    {
        "text": "Detroit",
        "relevance": "0.363931"
    },
    {
        "text": "Crocodile Hunter",
        "relevance": "0.350197"
    }
    ]
}

问题是关键字集合返回空值 - 尽管它似乎具有正确的大小,这是正面的。

2 个答案:

答案 0 :(得分:3)

这只是有效:

public class Keyword {
    public String text;
    public String relevance;
}


public class MyJSON {
    public String status;
    public String usage;
    public String language;
    public Collection<Keyword> keywords;
}

主要方法

        String str = "{\"status\": \"OK\","+
        "\"usage\": \"By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html\","+
        "\"url\": \"http://www.theage.com.au/world/aussie-trying-to-make-a-difference-gunned-down-20110510-1egnv.html\","+
        "\"language\": \"english\","+
        "\"keywords\": ["+
            "{"+
                "\"text\": \"Mr McNichols\","+
                "\"relevance\": \"0.99441\""+
            "},"+
            "{"+
                "\"text\": \"Ms Benton\","+
                "\"relevance\": \"0.392337\""+
            "},"+
            "{"+
                "\"text\": \"Detroit\","+
                "\"relevance\": \"0.363931\""+
            "},"+
            "{"+
                "\"text\": \"Crocodile Hunter\","+
                "\"relevance\": \"0.350197\""+
            "}"+
            "]"+
        "}";

        Gson gson = new Gson();
        MyJSON mj = gson.fromJson(str, MyJSON.class);
        System.out.println(mj.language);
        for(Keyword k: mj.keywords)
            System.out.println(k.text+":"+k.relevance);

打印

english
Mr McNichols:0.99441
Ms Benton:0.392337
Detroit:0.363931
Crocodile Hunter:0.350197

仔细查看我的Keyword类!(我的JSON字符串以{开头)。

答案 1 :(得分:0)

这完全取决于我自己的愚蠢。

在关键字类中,我调用变量word时应该是text,以便正确映射到JSON。