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.