I need help deserialising JSON in the following format in Java:
data.json
{
"tokens":[
{
"position":1,
"text":"hello",
"suggestions":[
{
"suggestion":"hi",
"points":0.534
},
{
"suggestion":"howdy",
"points":0.734
}
]
},
]
}
I've created two classes, one called Token and another called Suggestion, with attributes matching the JSON format.
Token.java
public class Token {
private int position;
private String text;
private List<Suggestion> suggestions;
public Token(int position, String text, List<Suggestion> suggestions) {
this.position = position;
this.text = text;
this.suggestions = suggestions;
}
}
Suggestion.java
public class Suggestion {
private String suggestion;
private double points;
public Suggestion(String suggestion, double points) {
this.suggestion = suggestion;
this.points = points;
}
}
How do I "unpack" the JSON into a list of Tokens, each of which has the two required strings and a list of Suggestion objects as its attributes? (Ideally, it would be using the Gson library)
I've tried this, but it doesn't work:
Gson gson = new Gson();
Type listType = new TypeToken<List<Token>>(){}.getType();
List<Token> tokenList = gson.fromJson(jsonString, listType);
System.out.println(tokenList.get(0));
Thanks