0

I haven't been programming for a long time but I like it and trying to get back on track. So please excuse the nature of the problem/question here.

What I need is pretty simple in my opinion but i'm mostly struggling with using gson and json-simple to read my json file, one at a time, and be able to retrieve the values.

I have seen many approaches on here but as I said been a while and I have not done Java a lot in my career. So need some guidance/explanation on the best approach.

JSON:

[{ "car": "Toyota", "colour": "red", "qty": "1","date_manufactured":"12972632260006" }, { "car": "Hyundai", "colour": "red", "qty": "2","date_manufactured":"1360421626000" }, { "car": "Kia", "colour": "blue", "qty": "2", "date_manufactured":"1265727226000"}, ]

Any help to put me on the right track is appreciated!

Zee
  • 172
  • 1
  • 5
  • 12

2 Answers2

5
  1. Create a POJO class to represent your JSON data:

    public class CarInfo {  
      String car;
      String colour;
      String qty;
      String date_manufactured; 
    }
    
  2. Use GSON to parse JSON String Array

    String carInfoJson = "[{ \"car\": \"Toyota\", \"colour\": \"red\",\"qty\": \"1\",\"date_manufactured\":\"12972632260006\" }, { \"car\":\"Hyundai\", \"colour\":\"red\",\"qty\":\"2\",\"date_manufactured\":\"1360421626000\" }]";
    Gson gson = new Gson();  
    CarInfo[] carInfoArray = gson.fromJson(carInfoJson, CarInfo[].class);  
    
  3. Use GSON to parse JSON String Array from a file

    String carInfoJson= new String(Files.readAllBytes(Paths.get("filename.txt")));
    Gson gson = new Gson();  
    CarInfo[] carInfoArray = gson.fromJson(carInfoJson, CarInfo[].class);
    
  4. Use GSON to parse JSON String Array from a file using BufferedReader

    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(file));
      Gson gson = new Gson(); 
      CarInfo[] carInfoArray = gson.fromJson(reader, CarInfo[].class);
    } catch (FileNotFoundException ex) {
      ...
    } finally {
      ...
    }
    
  5. Use GSON to parse JSON String Array from a file using JsonReader in stream mode

    try {
      InputStream stream = new FileInputStream("c:\\filename.txt");
      JsonReader reader = new JsonReader(new InputStreamReader(stream, "UTF-8"));
      Gson gson = new Gson();
    
      // Read file in stream mode
      reader.beginArray();
      while (reader.hasNext()) {
        CarInfo carInfo = gson.fromJson(reader, CarInfo.class);
      }
      reader.endArray();
      reader.close();
    } catch (UnsupportedEncodingException ex) {
      ...
    } catch (IOException ex) {
      ...
    }
    
Clive Seebregts
  • 2,004
  • 14
  • 18
  • if Im reading from a file i.e. `myFile = new BufferedReader(new FileReader(jsonFile));` `while ((line = myFile.readLine()) != null)` how would this approach be different ? or I can just pass "line" as is to to carInfoArray ? – Zee Feb 10 '18 at 16:39
  • What are the contents (output) of a line? – Clive Seebregts Feb 10 '18 at 16:44
  • first iteration I will get `[` second iteration I will get `{ "car": "Toyota", "colour": "red", "qty": "1","date_manufactured":"12972632260006" },` ans so on. My file format will always be valid and each line I read will be a block of data meaning whatever is between `{}`. Would your solution still be valid ? – Zee Feb 10 '18 at 16:56
  • If your entire file is one valid JSON entity, then you could just read it all into one large `String` and parse that. If you have a Collection of entities, make a POJO that reflects this. – qwelyt Feb 10 '18 at 17:03
  • the file is huge and my code must accommodate for small heap. Thats why I was wondering what is the best way to read the file once block of json at at a time, meaning one array of data `{}` at a time. So do you think passing just the array i.e `{}` to `gson.fromJson` will still be valid ? – Zee Feb 10 '18 at 17:11
  • @Zee see point 5 for reading and parsing files in stream mode – Clive Seebregts Feb 10 '18 at 17:29
  • @CliveSeebregts looks like I will go with your solution but still haven't got it to work as I keep getting "duplicate class: CarInfo" – Zee Feb 10 '18 at 18:00
  • @Zee not sure why as the code compiles without any errors. Perhaps you have two classes with the same name in the same package? – Clive Seebregts Feb 10 '18 at 18:32
0

I'm using json-simple to explain how to do this. Your json is a JSONArray (because it starts and ends with square brackets) with JSONObject (curly brackets with pair list) inside so first you've to extract the array using a JSONParser and then you can easily iterate over it and get fields from each JSONObject. Here is a simple example it just shows you an easy and understandable way:

String json = "[{ \"car\": \"Toyota\", \"colour\": \"red\", \"qty\": \"1\",\"date_manufactured\":\"12972632260006\" }, { \"car\": \"Hyundai\", \"colour\": \"red\", \"qty\": \"2\",\"date_manufactured\":\"1360421626000\" }]";
JSONParser parser = new JSONParser();
try {
    /* It's a JSONArray first. */
    JSONArray tmpArr = (JSONArray)parser.parse(json);
    for(Object obj : tmpArr){
        /* Extract each JSONObject */
        JSONObject tmpObj = (JSONObject) obj;
        System.out.println(tmpObj.get("car"));
        System.out.println(tmpObj.get("colour"));
        System.out.println(tmpObj.get("qty"));
        System.out.println(tmpObj.get("date_manufactured"));
    }
} catch (ParseException e) {
    e.printStackTrace();
}

Note that you can use Gson, it's much more complete then json-simple but a little bit trickier.

simo-r
  • 733
  • 1
  • 9
  • 13
  • if Im reading from a file i.e. myFile = new BufferedReader(new FileReader(jsonFile)); while ((line = myFile.readLine()) != null) how would this approach be different. I mean would I be able to read block of data at a time and use the same approach in your for loop to get the data out, from each Json set of data I get each time I loop through the file ? – Zee Feb 10 '18 at 16:44
  • The `parse()` method also accepts a `Reader` as parameter so you just have to check if the file respects the JSON syntax standards. – simo-r Feb 10 '18 at 16:53
  • in that case I will be passing just an array i.e. `{..}` every time I iterate through the json lines. Still valid for parser ? – Zee Feb 10 '18 at 16:59
  • This will read the whole file and will parse the whole array in the for-each iteration. If your file is too big you can use a reader to read a line or characters, parse them, get fields and then re-read. – simo-r Feb 10 '18 at 17:04
  • ok so i used `fHandler = new BufferedReader(new FileReader(jsonFile));` then passed to parser but will this load the full file ? I want to make sure to accommodate small heap with big files .. you approach is getting me some results but this the only part I need to make sure I have which is not to load all the file at once ? – Zee Feb 10 '18 at 17:23
  • This will parse the whole file so you will have the JSONArray with the full json inside. If your file is too big you have to read a bunch of characters/line/whatever and then use the parse method with string parameter. I would also remember you that's not a solve-my-problem community so try it yourself, we've given you tools and knowledge. – simo-r Feb 10 '18 at 17:28