0

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();

        }

    }

}
9
  • 1
    Where is your attempt? Commented Dec 13, 2018 at 14:23
  • @NicholasK my java code is now indicated. Commented Dec 13, 2018 at 14:32
  • 1
    The file as a whole is not valid JSON, instead it contains a sequence of JSON objects. Is there some easy rule to detect the boundaries between the objects? In your example it seems that each object is on a text line of its own. Commented Dec 13, 2018 at 14:36
  • @Henry i am stuck at navigating through the json sequences. Any help will be appreciated. Commented Dec 13, 2018 at 14:40
  • This does not answer my question. Commented Dec 13, 2018 at 14:41

1 Answer 1

1

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) {
                }
            });
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.