I have a file that contains a HashMap customer list in json format.
Like this:
{"Davide":{"name":"Davide","cf":"FRCDVD","pi":"1234",
"telephone":"333","website":"www","sector":"Student","address":"Rome"}}
This is just a one customer of list. Everytime the controller is called I want to take datas from the file and convert them into HashMap list.
I tried to do this with:
HashMap<String, Customer> listCustomer = new HashMap<>();
listCustomer = new ObjectMapper().readValue(pathCustomerFile, HashMap.class); //This line gives me error
I got this error:
org.codehaus.jackson.JsonParseException: Unexpected character ('/' (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)
How can I do that?
Please double check your input JSON string you don't have it wrongly escaped or
/
dangling somewhere. For example/\"name\"
. Then Provide the correct type mapping tis way:My answer was tested with jackson-mapper-asl 1.9.13.
** Your mapping just with
HashMap.class
will not give you desired results as Jackson will map your JSON intoMap<String, Map>
. You will find out, when you will try to get a value from Map and operate as if it were type of Customer.