bademba
bademba

Reputation: 247

Reading Json file with many json objects

I wish to read a json file with contents below in Java recursively. How can I achieve that?

{"preview":false,"result":{"TransactionType":"Mobile","TransactionID":"STSVSTFS7SVS3S","TransTime":"20181210171511"}}
{"preview":false,"result":{"TransactionType":"Mobile","TransactionID":"LKSNS6S2S7SVS3S","TransTime":"20181210171511"}}
{"preview":false,"result":{"TransactionType":"Mobile","TransactionID":"TSSKBDGD7SVS3S","TransTime":"20181210171511"}}

Here is my Java code except it only reads the first json

import com.brian.db.DBConnector;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


public class ReadJSONData {

    private static final String filePath = "D:\\test\\c2b.json";

    public static void main(String[] args) throws SQLException {
        try {
            // read the json file
            FileReader reader = new FileReader(filePath);
            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
            //System.out.println(jsonObject);
            Object preview =  jsonObject.get("preview");
            System.out.println("Preview:"+preview);

            JSONObject result = (JSONObject) jsonObject.get("result");
            System.out.println("RESULT:"+result);

            String transactionType =  (String) result.get("TransactionType");
            System.out.println("TransactionType:"+transactionType);

            String transid = (String) result.get("TransID");
            System.out.println("TransID:"+transid);

            String transTime =  (String) result.get("TransTime");
            System.out.println("TransTime:"+ transTime);



        } catch (FileNotFoundException ex) {

            ex.printStackTrace();

        } catch (IOException ex) {

            ex.printStackTrace();

        } catch (ParseException ex) {

            ex.printStackTrace();

        } catch (NullPointerException ex) {

            ex.printStackTrace();

        }

    }

}

Upvotes: 0

Views: 92

Answers (1)

madhead
madhead

Reputation: 33432

Just iterate over the lines and parse each one separately:

public class ReadJSONData {

    private static final String filePath = "D:\\test\\c2b.json";

    public static void main(String[] args) throws SQLException {
        JSONParser jsonParser = new JSONParser();
        try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
            stream.forEach(line -> {
                try {
                    JSONObject jsonObject = (JSONObject) jsonParser.parse(line);
                    …
                } catch (Exception e) {
                }
            });
        }
    }

}

Upvotes: 1

Related Questions