I would create a POJO for customers having the list with the customers and the getter/setter pair, like that:
class CustomersFile{
List<Customer> customers;
//getter and setter
}
Then I would create class Customer with the field name and customerDetails, like that:
class Customer {
String name;
CustomerDetails details;
//getters and setters for both fields
}
And finally I would create the class CustomerDetails with all the fields, like that:
class CustomerDetails {
String name;
String telephone;
int pi; // and so on
//getters and setters for all fields
}
Then using object mapper I would map all the customers from json to my CustomersFile object:
ObjectMapper mapper = new ObjectMapper();
//this configuration is needed in case you have only one customer in your json.
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
CustomersFile customersFile = mapper.readValue(pathCustomerFile, CustomersFile.class);
To access the list of customers, just call:
List<Customer> customers = customersFile.getCustomers();
If you really need a HashMap then loop through the list and fill this hash map:
HashMap<String, Customer> map = new HashMap<>();
for(Customer customer : customers) {
// as string you can use the id of the customer (pi) but its no necessary, just use your desired String
map.put("String.valueOf(customer.getPi())", customer);
}
UPDATE
Below are the dependencies I use in my project's pom:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
pathCustomerFile
is a String? I had a similar problem, I was wrong with the input parameter. Fixed by replacing withnew File (pathCustomerFile)