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();
        }
    }
\$\endgroup\$
  • \$\begingroup\$ Welcome! Was something specific you were hoping to get from a review? \$\endgroup\$ – Stephen Rauch Mar 10 '17 at 17:00
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>>() {}));
}
| improve this answer | |
\$\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();
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Vogel612 Mar 12 '17 at 11:33
  • \$\begingroup\$ @Vogel612 I agree, state.date should be stored in LocalDateTime instead of String, thanks for the feedback. \$\endgroup\$ – gaborage Mar 12 '17 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.