1

I've tried to read from a JSON-File for the first time (see code below...). Now my problem is, that my json file is auto-generated by rtl_433 and it is incomplete. In order for my code to work, I need to add the following in front and at the end of the JSON-File:

String front = "{"data":[";
String end = "]}"

Is there a way to do this in the code below? I thought about converting the data from the Filereader into a String and adding the two Strings to it before going on, processing it... I'ld also need to add "," in between a variable Number of JSON-Objects.. But it seems to me, that that's not possible.

I'm looking forwards to hearing your solution ideas! Please don't over-complicate it, I'm completely new, and coding is just a hobby - yet... :)

try{
            //Creating JSON-Parser
            JSONParser parser = new JSONParser();
            try {     
                ////Parsing the contents of the JSON file & casting received Object as JSON Object
                JSONObject obj = (JSONObject) parser.parse(new FileReader(path_json_file));
                JSONArray jsonArray = (JSONArray) obj.get("data");
                String[][] arr = new String[jsonArray.size()][3];
                
                SimpleDateFormat time_parser = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
                
                Date[] time = new Date[jsonArray.size()];
                
                System. out. println("Following data found in JSON-File:");
                
                for(int i=0; i<jsonArray.size(); i++){
                    
                    JSONObject jsonobj = (JSONObject) jsonArray.get(i);
    
                    
                    String time1 = (String) jsonobj.get("time");
                    try{
                        time[i] = time_parser.parse(time1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.print("  " + time1);

Here's a example of my JSON-File, the number of JSON-Objects varies:


{"time" : "2021-01-16 21:43:53", "model" : "Fineoffset-WH2A", "id" : 252, "temperature_C" : -3.000, "humidity" : 62, "mic" : "CRC"}
{"time" : "2021-01-16 21:43:54", "model" : "Bresser-3CH", "id" : 252, "temperature_C" : -17, "humidity" : 62, "mic" : "CRC"}
{"time" : "2021-01-18 21:43:53", "model" : "Bresser-3CH", "id" : 252, "temperature_C" : -3.500, "humidity" : 62, "mic" : "CRC"}
{"time" : "2021-01-17 14:16:34", "model" : "Bresser-3CH", "id" : 246, "channel" : 3, "battery_ok" : 0, "temperature_C" : -0.500, "humidity" : 86, "mic" : "CHECKSUM"}
{"time" : "2021-01-17 14:16:43", "model" : "Fineoffset-WH2A", "id" : 252, "temperature_C" : 1.200, "humidity" : 71, "mic" : "CRC"}
{"time" : "2021-01-17 14:16:49", "model" : "Bresser-3CH", "id" : 153, "channel" : 1, "battery_ok" : 0, "temperature_C" : 0.444, "humidity" : 81, "mic" : "CHECKSUM"}
1
  • you can convert the json object to string, then you can concat the json string at the front and last of the convert json string refer - stackoverflow.com/questions/17651395/… Commented Jan 17, 2021 at 13:52

1 Answer 1

1

What if you don't need to modify your file at all?

Here is my suggested solution which parses each line individually instead of treating the entire file content as a json.

 public static void main(String... args) {
    try {
        JSONParser parser = new JSONParser();
        List<Date> datesFromJsonFile = Files.lines(Paths.get("path/to/file.json"))
                                            .map(jsonLine -> parseSingleLine(parser, jsonLine))
                                            .map(jsonObject -> (String) jsonObject.get("time"))
                                            .map(time -> parseDate(time))
                                            .collect(Collectors.toList());

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static Date parseDate(String time) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");

    try {
        return sdf.parse(time);
    } catch (java.text.ParseException e) {
        throw new RuntimeException("Cannot parse date", e);
    }
}

private static JSONObject parseSingleLine(JSONParser parser, String jsonLine) {
    try {
        return (JSONObject) parser.parse(jsonLine);
    } catch (ParseException e) {
        throw new RuntimeException("Cannot parse json", 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.