I have a JSON file
{
"measurements": [
{
"time": "100416",
"temp": "7.64",
},
{
"time": "110416",
"temp": " 7.76 ",
},
{
"time": "120416",
"temp": " 7.86 ",
}
]
}
and I need to save the values as class objects( or Hashmap) with a key value of "time". Then while typing "time" for example: 120416 It will print the temp for that time. I'm using Netbeans.
This is where I'm so far. I can print an Array:
public class Weathers {
private static final String filePath = "C:\\measurements.json";
public static void main(String[] args) {
try {
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
JSONArray JArray = (JSONArray) jsonObject.get("measurements");
for (int i =0;i <JArray.size();i++)
{
System.out.println(JArray.get(i));
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
}