I am using Java to parse a JSON file into a hash map so that i can search by typing a date and then get the cost, tax and profit for that date.
So that if i write:
2001年09月18日 13:11:01
I get:
7.14, 1.81 and 31.10
Here is part of the JSON file:
{
"stat":[
{
"date":" 2001年09月18日 13:11:01 ",
"cost":" 7.14 ",
"tax":" 1.81 ",
"profit":" 31.10 "
},
{
"date":" 2001年09月18日 14:15:02 ",
"cost":" 7.80 ",
"tax":" 0.99 ",
"profit":" 30.20 "
}
]
}
And my code using Jackson:
public static void main(String[] args) throws JsonGenerationException {
try {
ObjectMapper mapper = new ObjectMapper();
JSONParser parser = new JSONParser();
JSONObject jsonobj = (JSONObject) parser.parse(new FileReader("test.json"));
JSONArray jsons = (JSONArray) jsonobj.get("stat");
Map<Integer, Map<String, String>> jsonmaps = new HashMap<Integer, Map<String, String>>();
int i=0;
for (Object j : jsons) {
Map<String, String> map = new HashMap<String, String>();
JSONObject o = (JSONObject) j;
map = mapper.readValue(j.toString(), new TypeReference<Map<String, String>>() {
});
jsonmaps.put(i, map);
i++;
}
System.out.println(jsonmaps.get(0).get("date"));
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
-
\$\begingroup\$ Welcome! Was something specific you were hoping to get from a review? \$\endgroup\$Stephen Rauch– Stephen Rauch2017年03月10日 17:00:03 +00:00Commented Mar 10, 2017 at 17:00
2 Answers 2
The initial value of a map
variable is not used, you can avoid this assignment and inline the variable. o
is not used too.
If indexes in a map is consequent then it is better to use a List
instead of Map
. So the part of the code can be rewritten following way:
List<Map<String, String>> jsonmaps = new ArrayList<>();
for (Object j : jsons) {
jsonmaps.add(mapper.readValue(j.toString(),
new TypeReference<Map<String, String>>() {}));
}
If you use a POJO, your code could be reduced to:
public class Stats {
public List<Stat> stat;
}
public class Stat {
public String date;
public Double cost;
public Double tax;
public Double profit;
}
public static void main(String[] args)
ObjectMapper mapper = new ObjectMapper();
try {
Stats stats = mapper.readValue(new File("test.json"), Stats.class);
System.out.println(stats.stat.get(0).date);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
-
\$\begingroup\$ storing a date in a
String
is generally a bad idea, especially since the stats are looked up by their date, which means your code can result in significant lookup time increases ... The POJO is a good idea, though. AMap<Date, Stat>
is easily creatable from the list you have there \$\endgroup\$Vogel612– Vogel6122017年03月12日 11:33:51 +00:00Commented Mar 12, 2017 at 11:33 -
\$\begingroup\$ @Vogel612 I agree, state.date should be stored in LocalDateTime instead of String, thanks for the feedback. \$\endgroup\$gaborage– gaborage2017年03月12日 23:25:32 +00:00Commented Mar 12, 2017 at 23:25