I have a file that contains a HashMap customer list in json format.
我有一个包含json格式的HashMap客户列表的文件。
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.
这只是列表中的一个客户。每次调用控制器时,我都希望从文件中获取数据并将其转换为HashMap列表。
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)
org.codehaus.jackson.JsonParseException:意外字符('/'(代码47)):也许是(非标准)注释? (因为功能'ALLOW_COMMENTS'未启用解析器而未被识别为一个)
How can I do that?
我怎样才能做到这一点?
3 个解决方案
#1
8
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.
我最近有这个问题。我试图将路径(以字符串的形式)传递给readValue。您需要将其传递给要解析的字符串或文件对象。基于您的变量名称我认为您可能已将其传递给文件的路径。
(Basically, it's reading the '/' in the file path and throwing errors on them.
(基本上,它正在读取文件路径中的'/'并在它们上面抛出错误。
#2
1
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:
请仔细检查您输入的JSON字符串,您没有错误地将其转义或/悬挂在某处。例如/ \“name \”。然后提供正确的类型映射方式:
new ObjectMapper().readValue(pathCustomerFile, new TypeReference<HashMap<String, Customer>>(){});
My answer was tested with jackson-mapper-asl 1.9.13.
我的答案是用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.
**你的HashMap.class映射不会给你想要的结果,因为Jackson会将你的JSON映射到Map
#3
1
I would create a POJO for customers having the list with the customers and the getter/setter pair, like that:
我会为拥有客户列表和getter / setter对的客户创建一个POJO,如下所示:
class CustomersFile{
List<Customer> customers;
//getter and setter
}
Then I would create class Customer with the field name and customerDetails, like that:
然后我将使用字段名称和customerDetails创建类Customer,如下所示:
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:
最后,我将使用所有字段创建类CustomerDetails,如下所示:
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:
然后使用对象映射器,我将所有客户从json映射到我的CustomersFile对象:
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,那么循环遍历列表并填充这个哈希映射:
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:
以下是我在项目的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>
#1
8
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.
我最近有这个问题。我试图将路径(以字符串的形式)传递给readValue。您需要将其传递给要解析的字符串或文件对象。基于您的变量名称我认为您可能已将其传递给文件的路径。
(Basically, it's reading the '/' in the file path and throwing errors on them.
(基本上,它正在读取文件路径中的'/'并在它们上面抛出错误。
#2
1
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:
请仔细检查您输入的JSON字符串,您没有错误地将其转义或/悬挂在某处。例如/ \“name \”。然后提供正确的类型映射方式:
new ObjectMapper().readValue(pathCustomerFile, new TypeReference<HashMap<String, Customer>>(){});
My answer was tested with jackson-mapper-asl 1.9.13.
我的答案是用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.
**你的HashMap.class映射不会给你想要的结果,因为Jackson会将你的JSON映射到Map
#3
1
I would create a POJO for customers having the list with the customers and the getter/setter pair, like that:
我会为拥有客户列表和getter / setter对的客户创建一个POJO,如下所示:
class CustomersFile{
List<Customer> customers;
//getter and setter
}
Then I would create class Customer with the field name and customerDetails, like that:
然后我将使用字段名称和customerDetails创建类Customer,如下所示:
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:
最后,我将使用所有字段创建类CustomerDetails,如下所示:
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:
然后使用对象映射器,我将所有客户从json映射到我的CustomersFile对象:
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,那么循环遍历列表并填充这个哈希映射:
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:
以下是我在项目的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>