Org.codehaus.jackson.JsonParseException: Unexpected character ('/' (code 47))
I have a file that contains a list of HashMap clients in json format.
Like this:
{"Davide":{"name":"Davide","cf":"FRCDVD","pi":"1234",
"telephone":"333","website":"www","sector":"Student","address":"Rome"}}
This is just one client list. Every time the controller is called, I want to take data from the file and convert it to a HashMap list.
I tried to do it 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)): Possible (non-standard) comment? (not recognized as one as the "ALLOW_COMMENTS" function is not enabled for the parser)
How can i do this?
source to share
I have had this problem lately. I was trying to pass the path (as a string) for readValue. You need to pass either a string to parse or a file object. Based on your variable name, I think you may have passed the file path to it.
(Basically, it reads "/" in the file path and throws errors at them.
source to share
Please double check the JSON input string you are not wrong or /
dangle somewhere. For example /\"name\"
. Then specify the correct type mapping as follows:
new ObjectMapper().readValue(pathCustomerFile, new TypeReference<HashMap<String, Customer>>(){});
My answer was tested with jackson-mapper-asl 1.9.13.
** Your matching with only HashMap.class
will not give the desired results as Jackson will map your JSON to Map<String, Map>
. You will know when you try to get a value from the card and act as if it were a Client type.
source to share
I would create a POJO for clients having a list with clients and a getter / setter pair, for example:
class CustomersFile{
List<Customer> customers;
//getter and setter
}
Then I would create a Customer class with the field name and customerDetails like:
class Customer {
String name;
CustomerDetails details;
//getters and setters for both fields
}
And finally, I would create a CustomerDetails class with all fields, for example:
class CustomerDetails {
String name;
String telephone;
int pi; // and so on
//getters and setters for all fields
}
Then, using mapperper objects, I would map all 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 your customer list, simply call:
List<Customer> customers = customersFile.getCustomers();
If you really want a HashMap, then scroll through the list and fill in this hashmap:
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 am using in my pom project:
<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>
source to share