3
\$\begingroup\$

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();
 }
 }
konijn
34.3k5 gold badges71 silver badges267 bronze badges
asked Mar 10, 2017 at 16:49
\$\endgroup\$
1
  • \$\begingroup\$ Welcome! Was something specific you were hoping to get from a review? \$\endgroup\$ Commented Mar 10, 2017 at 17:00

2 Answers 2

1
\$\begingroup\$

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>>() {}));
}
answered Mar 10, 2017 at 18:24
\$\endgroup\$
1
\$\begingroup\$

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();
 }
}
answered Mar 12, 2017 at 5:04
\$\endgroup\$
2
  • \$\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. A Map<Date, Stat> is easily creatable from the list you have there \$\endgroup\$ Commented 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\$ Commented Mar 12, 2017 at 23:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.