store sensor data to json files
parent
7007787848
commit
629ab30668
@ -0,0 +1,21 @@
|
||||
package sh.adb.sensorCommunityAPI;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DataStore {
|
||||
public boolean storeJSON(String path, JSONObject json) throws IOException {
|
||||
FileWriter fw = new FileWriter(path);
|
||||
try {
|
||||
System.out.println(json.toString());
|
||||
fw.write(json.toString());
|
||||
fw.close();
|
||||
return true;
|
||||
}catch (IOException e){
|
||||
System.out.println("error wile soring data to: " + path);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,12 +1,34 @@
|
||||
package sh.adb.sensorCommunityAPI;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException, ParseException {
|
||||
String basePath = "./data";
|
||||
|
||||
APIPaser api = new APIPaser("https://data.sensor.community/airrohr/v1/sensor/35943/");
|
||||
api.getJSON();
|
||||
JSONArray APIData = api.getJSONObject();
|
||||
JSONObject entry = (JSONObject) APIData.get(0);
|
||||
System.out.println("entry => ");
|
||||
System.out.println(entry);
|
||||
|
||||
//get country
|
||||
String country = ((JSONObject) entry.get("location")).get("country").toString();
|
||||
|
||||
//get sensorID
|
||||
String sensorID = String.valueOf(
|
||||
((JSONObject) entry.get("sensor")).get("id")
|
||||
);
|
||||
|
||||
//get timestamp
|
||||
String timestamp = (String) entry.get("timestamp");
|
||||
|
||||
String path = basePath + country + "-" + sensorID + "-" + timestamp + ".json";
|
||||
DataStore store = new DataStore();
|
||||
store.storeJSON(path, entry);
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +0,0 @@
|
||||
package sh.adb.sensorCommunityAPI;
|
||||
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class dataStore {
|
||||
public boolean storeJson(String path, JSONObject json) throws IOException {
|
||||
boolean check = false;
|
||||
|
||||
FileWriter file = new FileWriter(path);
|
||||
try {
|
||||
file.write(json.toJSONString());
|
||||
check = true;
|
||||
}catch (IOException e){
|
||||
System.out.println("Sorry cant write in file" + path);
|
||||
}
|
||||
return check;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue