how to parse JSON file with GSON
JavaJsonGsonJava Problem Overview
I have a very simple JSON with reviews for products, like:
{
"reviewerID": "A2XVJBSRI3SWDI",
"asin": "0000031887",
"reviewerName": "abigail",
"helpful": [0, 0],
"unixReviewTime": 1383523200,
"reviewText": "Perfect red tutu for the price. ",
"overall": 5.0,
"reviewTime": "11 4, 2013", "summary": "Nice tutu"
}
{
"reviewerID": "A2G0LNLN79Q6HR",
"asin": "0000031887",
"reviewerName": "aj_18 \"Aj_18\"",
"helpful": [1, 1],
"unixReviewTime": 1337990400,
"reviewText": "This was a really cute",
"overall": 4.0,
"reviewTime": "05 26, 2012",
"summary": "Really Cute but rather short."
}
I'd like to read it into my Java app using GSON. I have built a class to hold results for each review:
public class Review {
private String reviewerID;
private String asin;
private String reviewerName;
private ArrayList<Integer> helpful;
private String reviewText;
private Double overall;
private String summary;
private Long unixReviewTime;
private String reviewTime;
public Review() {
this.helpful = Lists.newArrayList();
}
// some getters and setters...
To read the JSON file, my code is:
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader(filename));
Review data = gson.fromJson(reader, Review.class);
data.toScreen(); // prints to screen some values
With this code, I can only retrieve the first review in the JSON, so my question is: how to iterate through all the reader and get the next reviews? I don't need to store the reviews in a List
Java Solutions
Solution 1 - Java
You have to fetch the whole data in the list and then do the iteration as it is a file and will become inefficient otherwise.
private static final Type REVIEW_TYPE = new TypeToken<List<Review>>() {
}.getType();
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader(filename));
List<Review> data = gson.fromJson(reader, REVIEW_TYPE); // contains the whole reviews list
data.toScreen(); // prints to screen some values
Solution 2 - Java
just parse as an array:
Review[] reviews = new Gson().fromJson(jsonString, Review[].class);
then if you need you can also create a list in this way:
List<Review> asList = Arrays.asList(reviews);
P.S. your json string should be look like this:
[ { "reviewerID": "A2SUAM1J3GNN3B1", "asin": "0000013714", "reviewerName": "J. McDonald", "helpful": [2, 3],
"reviewText": "I bought this for my husband who plays the piano.",
"overall": 5.0,
"summary": "Heavenly Highway Hymns",
"unixReviewTime": 1252800000,
"reviewTime": "09 13, 2009"
},
{
"reviewerID": "A2SUAM1J3GNN3B2",
"asin": "0000013714",
"reviewerName": "J. McDonald",
"helpful": [2, 3],
"reviewText": "I bought this for my husband who plays the piano.",
"overall": 5.0,
"summary": "Heavenly Highway Hymns",
"unixReviewTime": 1252800000,
"reviewTime": "09 13, 2009"
},
[...]
]
Solution 3 - Java
In case you need to parse it from a file, I find the best solution to use a HashMap<String, String>
to use it inside your java code for better manipultion.
Try out this code:
public HashMap<String, String> myMethodName() throws FileNotFoundException
{
String path = "absolute path to your file";
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
Gson gson = new Gson();
HashMap<String, String> json = gson.fromJson(bufferedReader, HashMap.class);
return json;
}
Solution 4 - Java
;to_upper: ; push ebp ; mov ebp, esp ; ;; void to_upper(char* array, int n) ; mov eax, [ebp + 8]; "Anna" ; mov ebx, [ebp + 12] ; 4
; 65
; 97 - 32
; xor ecx, ecx; 0 ; for: ; cmp ecx, ebx ; jl big ; je exit
; big:
; mov dl, byte [eax + ecx]
; cmp dl, 97
; jg decrease
; jl cont
;
; decrease:
; sub edx, 32 ; n -> N
; mov byte [eax + ecx], byte dl
; inc ecx
; jmp for
;
; cont:
; inc ecx
; jmp for;
;
;exit:
; leave
; ret
; leave ; ret
main: push ebp mov ebp, esp ; ; push 4 ; push anna ; call to_upper ;add esp, 8
; push string ; push scanf_fmt ; call scanf ; add esp, 8
; push 4 ; push string ; call to_upper ; add esp, 8
; push eax ; call puts ; add esp, 4