how to parse JSON file with GSON










36















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, just need to access the object once. Any help more than welcome.










share|improve this question
























  • Parse the string you receive into a new JSONArray(). For each object in the array, do gson.fromJson(object, Review.class), then add them all to an empty List<Review> list = new LinkedList<Review>(); As in, parse them all sequentially.

    – Schaka
    Apr 30 '15 at 10:57















36















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, just need to access the object once. Any help more than welcome.










share|improve this question
























  • Parse the string you receive into a new JSONArray(). For each object in the array, do gson.fromJson(object, Review.class), then add them all to an empty List<Review> list = new LinkedList<Review>(); As in, parse them all sequentially.

    – Schaka
    Apr 30 '15 at 10:57













36












36








36


8






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, just need to access the object once. Any help more than welcome.










share|improve this question
















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, just need to access the object once. Any help more than welcome.







java json gson






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 30 '15 at 18:26







user299791

















asked Apr 30 '15 at 10:50









user299791user299791

1,04011739




1,04011739












  • Parse the string you receive into a new JSONArray(). For each object in the array, do gson.fromJson(object, Review.class), then add them all to an empty List<Review> list = new LinkedList<Review>(); As in, parse them all sequentially.

    – Schaka
    Apr 30 '15 at 10:57

















  • Parse the string you receive into a new JSONArray(). For each object in the array, do gson.fromJson(object, Review.class), then add them all to an empty List<Review> list = new LinkedList<Review>(); As in, parse them all sequentially.

    – Schaka
    Apr 30 '15 at 10:57
















Parse the string you receive into a new JSONArray(). For each object in the array, do gson.fromJson(object, Review.class), then add them all to an empty List<Review> list = new LinkedList<Review>(); As in, parse them all sequentially.

– Schaka
Apr 30 '15 at 10:57





Parse the string you receive into a new JSONArray(). For each object in the array, do gson.fromJson(object, Review.class), then add them all to an empty List<Review> list = new LinkedList<Review>(); As in, parse them all sequentially.

– Schaka
Apr 30 '15 at 10:57












3 Answers
3






active

oldest

votes


















62














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





share|improve this answer


















  • 3





    I have this error with your code: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

    – user299791
    Apr 30 '15 at 11:20






  • 1





    check whether your json file is in correct format or not at jsonlint

    – Archit Maheshwari
    Apr 30 '15 at 11:34







  • 1





    @Archit Maheshwari - The input is correctly formatted and is valid. It contains two JSON data structures of type 'object'. It is your example that is not working, at least not with gson 2.8.0. Nonetheless, I believe your example provides an interesting approach how to solve the problem. I suggest to add it to Gson's wishlist.

    – whaefelinger
    Feb 14 '17 at 10:59






  • 2





    Why using type instead of just Review data = gson.fromJson(reader, Review.class);?

    – Line
    May 17 '17 at 10:48



















20














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"
,

[...]
]





share|improve this answer

























  • I get an error with your code: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $

    – user299791
    Apr 30 '15 at 11:18






  • 1





    show us the first lines of your REAL json string

    – dit
    Apr 30 '15 at 11:26












  • sorry my file is 3GB, I tried to load with VIM but cannot load it

    – user299791
    Apr 30 '15 at 12:38












  • just show me the first bytes of the file. I suppose your json string is not valid

    – dit
    Apr 30 '15 at 12:42











  • man, if you tell me how to do it, I would love to!!! I am on Mac OSX... do you have a line I can copy in the terminal to show it?

    – user299791
    Apr 30 '15 at 12:44


















1














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;






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29965764%2fhow-to-parse-json-file-with-gson%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    62














    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





    share|improve this answer


















    • 3





      I have this error with your code: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

      – user299791
      Apr 30 '15 at 11:20






    • 1





      check whether your json file is in correct format or not at jsonlint

      – Archit Maheshwari
      Apr 30 '15 at 11:34







    • 1





      @Archit Maheshwari - The input is correctly formatted and is valid. It contains two JSON data structures of type 'object'. It is your example that is not working, at least not with gson 2.8.0. Nonetheless, I believe your example provides an interesting approach how to solve the problem. I suggest to add it to Gson's wishlist.

      – whaefelinger
      Feb 14 '17 at 10:59






    • 2





      Why using type instead of just Review data = gson.fromJson(reader, Review.class);?

      – Line
      May 17 '17 at 10:48
















    62














    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





    share|improve this answer


















    • 3





      I have this error with your code: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

      – user299791
      Apr 30 '15 at 11:20






    • 1





      check whether your json file is in correct format or not at jsonlint

      – Archit Maheshwari
      Apr 30 '15 at 11:34







    • 1





      @Archit Maheshwari - The input is correctly formatted and is valid. It contains two JSON data structures of type 'object'. It is your example that is not working, at least not with gson 2.8.0. Nonetheless, I believe your example provides an interesting approach how to solve the problem. I suggest to add it to Gson's wishlist.

      – whaefelinger
      Feb 14 '17 at 10:59






    • 2





      Why using type instead of just Review data = gson.fromJson(reader, Review.class);?

      – Line
      May 17 '17 at 10:48














    62












    62








    62







    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





    share|improve this answer













    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 30 '15 at 10:59









    Archit MaheshwariArchit Maheshwari

    794612




    794612







    • 3





      I have this error with your code: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

      – user299791
      Apr 30 '15 at 11:20






    • 1





      check whether your json file is in correct format or not at jsonlint

      – Archit Maheshwari
      Apr 30 '15 at 11:34







    • 1





      @Archit Maheshwari - The input is correctly formatted and is valid. It contains two JSON data structures of type 'object'. It is your example that is not working, at least not with gson 2.8.0. Nonetheless, I believe your example provides an interesting approach how to solve the problem. I suggest to add it to Gson's wishlist.

      – whaefelinger
      Feb 14 '17 at 10:59






    • 2





      Why using type instead of just Review data = gson.fromJson(reader, Review.class);?

      – Line
      May 17 '17 at 10:48













    • 3





      I have this error with your code: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

      – user299791
      Apr 30 '15 at 11:20






    • 1





      check whether your json file is in correct format or not at jsonlint

      – Archit Maheshwari
      Apr 30 '15 at 11:34







    • 1





      @Archit Maheshwari - The input is correctly formatted and is valid. It contains two JSON data structures of type 'object'. It is your example that is not working, at least not with gson 2.8.0. Nonetheless, I believe your example provides an interesting approach how to solve the problem. I suggest to add it to Gson's wishlist.

      – whaefelinger
      Feb 14 '17 at 10:59






    • 2





      Why using type instead of just Review data = gson.fromJson(reader, Review.class);?

      – Line
      May 17 '17 at 10:48








    3




    3





    I have this error with your code: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

    – user299791
    Apr 30 '15 at 11:20





    I have this error with your code: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

    – user299791
    Apr 30 '15 at 11:20




    1




    1





    check whether your json file is in correct format or not at jsonlint

    – Archit Maheshwari
    Apr 30 '15 at 11:34






    check whether your json file is in correct format or not at jsonlint

    – Archit Maheshwari
    Apr 30 '15 at 11:34





    1




    1





    @Archit Maheshwari - The input is correctly formatted and is valid. It contains two JSON data structures of type 'object'. It is your example that is not working, at least not with gson 2.8.0. Nonetheless, I believe your example provides an interesting approach how to solve the problem. I suggest to add it to Gson's wishlist.

    – whaefelinger
    Feb 14 '17 at 10:59





    @Archit Maheshwari - The input is correctly formatted and is valid. It contains two JSON data structures of type 'object'. It is your example that is not working, at least not with gson 2.8.0. Nonetheless, I believe your example provides an interesting approach how to solve the problem. I suggest to add it to Gson's wishlist.

    – whaefelinger
    Feb 14 '17 at 10:59




    2




    2





    Why using type instead of just Review data = gson.fromJson(reader, Review.class);?

    – Line
    May 17 '17 at 10:48






    Why using type instead of just Review data = gson.fromJson(reader, Review.class);?

    – Line
    May 17 '17 at 10:48














    20














    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"
    ,

    [...]
    ]





    share|improve this answer

























    • I get an error with your code: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $

      – user299791
      Apr 30 '15 at 11:18






    • 1





      show us the first lines of your REAL json string

      – dit
      Apr 30 '15 at 11:26












    • sorry my file is 3GB, I tried to load with VIM but cannot load it

      – user299791
      Apr 30 '15 at 12:38












    • just show me the first bytes of the file. I suppose your json string is not valid

      – dit
      Apr 30 '15 at 12:42











    • man, if you tell me how to do it, I would love to!!! I am on Mac OSX... do you have a line I can copy in the terminal to show it?

      – user299791
      Apr 30 '15 at 12:44















    20














    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"
    ,

    [...]
    ]





    share|improve this answer

























    • I get an error with your code: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $

      – user299791
      Apr 30 '15 at 11:18






    • 1





      show us the first lines of your REAL json string

      – dit
      Apr 30 '15 at 11:26












    • sorry my file is 3GB, I tried to load with VIM but cannot load it

      – user299791
      Apr 30 '15 at 12:38












    • just show me the first bytes of the file. I suppose your json string is not valid

      – dit
      Apr 30 '15 at 12:42











    • man, if you tell me how to do it, I would love to!!! I am on Mac OSX... do you have a line I can copy in the terminal to show it?

      – user299791
      Apr 30 '15 at 12:44













    20












    20








    20







    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"
    ,

    [...]
    ]





    share|improve this answer















    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"
    ,

    [...]
    ]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 30 '15 at 11:32

























    answered Apr 30 '15 at 11:02









    ditdit

    5,71743050




    5,71743050












    • I get an error with your code: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $

      – user299791
      Apr 30 '15 at 11:18






    • 1





      show us the first lines of your REAL json string

      – dit
      Apr 30 '15 at 11:26












    • sorry my file is 3GB, I tried to load with VIM but cannot load it

      – user299791
      Apr 30 '15 at 12:38












    • just show me the first bytes of the file. I suppose your json string is not valid

      – dit
      Apr 30 '15 at 12:42











    • man, if you tell me how to do it, I would love to!!! I am on Mac OSX... do you have a line I can copy in the terminal to show it?

      – user299791
      Apr 30 '15 at 12:44

















    • I get an error with your code: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $

      – user299791
      Apr 30 '15 at 11:18






    • 1





      show us the first lines of your REAL json string

      – dit
      Apr 30 '15 at 11:26












    • sorry my file is 3GB, I tried to load with VIM but cannot load it

      – user299791
      Apr 30 '15 at 12:38












    • just show me the first bytes of the file. I suppose your json string is not valid

      – dit
      Apr 30 '15 at 12:42











    • man, if you tell me how to do it, I would love to!!! I am on Mac OSX... do you have a line I can copy in the terminal to show it?

      – user299791
      Apr 30 '15 at 12:44
















    I get an error with your code: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $

    – user299791
    Apr 30 '15 at 11:18





    I get an error with your code: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $

    – user299791
    Apr 30 '15 at 11:18




    1




    1





    show us the first lines of your REAL json string

    – dit
    Apr 30 '15 at 11:26






    show us the first lines of your REAL json string

    – dit
    Apr 30 '15 at 11:26














    sorry my file is 3GB, I tried to load with VIM but cannot load it

    – user299791
    Apr 30 '15 at 12:38






    sorry my file is 3GB, I tried to load with VIM but cannot load it

    – user299791
    Apr 30 '15 at 12:38














    just show me the first bytes of the file. I suppose your json string is not valid

    – dit
    Apr 30 '15 at 12:42





    just show me the first bytes of the file. I suppose your json string is not valid

    – dit
    Apr 30 '15 at 12:42













    man, if you tell me how to do it, I would love to!!! I am on Mac OSX... do you have a line I can copy in the terminal to show it?

    – user299791
    Apr 30 '15 at 12:44





    man, if you tell me how to do it, I would love to!!! I am on Mac OSX... do you have a line I can copy in the terminal to show it?

    – user299791
    Apr 30 '15 at 12:44











    1














    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;






    share|improve this answer





























      1














      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;






      share|improve this answer



























        1












        1








        1







        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;






        share|improve this answer















        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;







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 11:05









        qiAlex

        2,0261724




        2,0261724










        answered Nov 13 '18 at 9:50









        Constantin TrepadusConstantin Trepadus

        446




        446



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29965764%2fhow-to-parse-json-file-with-gson%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo