开发者

Using Gson to unserialize an array of objects

开发者 https://www.devze.com 2023-03-05 12:09 出处:网络
I have a JSON response that looks like this. I want to extract the values of \"text\" and put them into a Set of Strings (i.e. I don\'t necessarily need the entire JSON to be derialised).

I have a JSON response that looks like this. I want to extract the values of "text" and put them into a Set of Strings (i.e. I don't necessarily need the entire JSON to be derialised).

I am using the GSON library

So far my method looks like this (It's obviously wrong):

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

I am calling deserialise with this:

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 is a class with the following member variables (with getters and setters):

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

Keyword is a class with the following member variables (with getters and setters):

private String word;
private String relevance;

The JSON looks like this:

{
"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"
    }
    ]
}

The problem is that the Collection of Keywords returns null values - although it seems to have the correct size, which is positive.


This just works:

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;
}

In the main method

        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);

This prints

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

Look carefully at my Keyword class!(and my JSON string starts with {).


This was all down to my own stupidity.

In the keyword class I called the variable word when it should have been text in order to map properly to the JSON.

0

精彩评论

暂无评论...
验证码 换一张
取 消