Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Gson to unserialize an array of objects
    primarykey
    data
    text
    <p>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).</p> <p>I am using the <a href="http://code.google.com/p/google-gson/" rel="nofollow">GSON</a> library </p> <p>So far my method looks like this (It's obviously wrong):</p> <pre><code> public static Response deserialise(String json){ Gson gson = new Gson(); Response r = gson.fromJson(json, Response.class); return r; } </code></pre> <p>I am calling <code>deserialise</code> with this:</p> <pre><code>Response r = deserialise(json); System.out.println("[status]: "+r.getStatus()); // works fine Collection&lt;Keyword&gt; coll = r.getKeywords(); Iterator&lt;Keyword&gt; itr = coll.iterator(); while(itr.hasNext()){ System.out.println(itr.next().getWord()); //prints null every time } </code></pre> <p>Response is a class with the following member variables (with getters and setters):</p> <pre><code>private String status; private String usage; private String language; private Collection&lt;Keyword&gt; keywords; </code></pre> <p>Keyword is a class with the following member variables (with getters and setters):</p> <pre><code>private String word; private String relevance; </code></pre> <p>The JSON looks like this:</p> <pre><code>{ "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" } ] } </code></pre> <p>The problem is that the Collection of Keywords returns null values - although it seems to have the correct size, which is positive.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload