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