Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.codehaus.jackson.JsonParseException: Unexpected character ('/' (code 47))

Tags:

java

json

hashmap

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?

like image 333
Dave Avatar asked Jun 12 '15 09:06

Dave


3 Answers

I had this issue recently. I was trying to pass a path (in the form of a string) to readValue. You need to pass it either a string to parse, or a file object. Based on your variable nameI think you may have passed it the path to a file.

(Basically, it's reading the '/' in the file path and throwing errors on them.

like image 129
Steve Avatar answered Nov 04 '22 21:11

Steve


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:

new ObjectMapper().readValue(pathCustomerFile, new TypeReference<HashMap<String, Customer>>(){});

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 into Map<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.

like image 28
zbig Avatar answered Nov 04 '22 21:11

zbig


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>
like image 29
Arthur Eirich Avatar answered Nov 04 '22 20:11

Arthur Eirich