org.codehaus.jackson.JsonParseException:意外的字符(' /'(代码47))

时间:2015-06-12 09:25:14

标签: java json hashmap

我有一个包含json格式的HashMap客户列表的文件。

像这样:

{"Davide":{"name":"Davide","cf":"FRCDVD","pi":"1234",
    "telephone":"333","website":"www","sector":"Student","address":"Rome"}}

这只是列表的一个客户。 每次调用控制器时,我都想从文件中获取数据并将它们转换为HashMap列表。

我试着这样做:

HashMap<String, Customer> listCustomer = new HashMap<>();
listCustomer = new ObjectMapper().readValue(pathCustomerFile, HashMap.class); //This line gives me error

我收到了这个错误:

  

org.codehaus.jackson.JsonParseException:意外字符(&#39; /&#39;(代码47)):也许是(非标准)评论? (因为功能&#39; ALLOW_COMMENTS&#39;未启用解析器,因此无法识别)

我该怎么做?

5 个答案:

答案 0 :(得分:14)

我最近遇到过这个问题。我试图将路径(以字符串的形式)传递给readValue。您需要将其传递给要解析的字符串或文件对象。基于您的变量名称我认为您可能已将其传递给文件的路径。

(基本上,它正在阅读文件路径中的&#39; /&#39;并在其上抛出错误。

答案 1 :(得分:1)

请仔细检查您输入的JSON字符串,您没有错误地将其转义或 <?php $query="SELECT question FROM poll ORDER BY id DESC LIMIT 1"; $results = mysql_query($query); while ($row = mysql_fetch_array($results)) { echo '<tr>'; foreach($row as $field) { echo '<td>' . htmlspecialchars($field) . '</td>'; } echo '</tr>'; } ?> 悬挂在某处。例如/。然后提供正确的类型映射:

/\"name\"

我的答案是用jackson-mapper-asl 1.9.13测试的。

**由于杰克逊将您的JSON映射到new ObjectMapper().readValue(pathCustomerFile, new TypeReference<HashMap<String, Customer>>(){}); ,因此仅使用HashMap.class的映射无法获得所需的结果。你会发现,当你试图从Map获得一个值并且像它是客户类型那样运作时。

答案 2 :(得分:1)

我会为拥有客户列表和getter / setter对的客户创建一个POJO,如下所示:

class CustomersFile{
  List<Customer> customers;

  //getter and setter
}

然后我将使用字段名称和customerDetails创建类Customer,如下所示:

class Customer {
  String name;
  CustomerDetails details;

  //getters and setters for both fields
}

最后,我将创建具有所有字段的CustomerDetails类,如:

class CustomerDetails {
  String name;
  String telephone;
  int pi; // and so on

  //getters and setters for all fields  
}

然后使用对象映射器,我将所有客户从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);

要访问客户列表,只需致电:

List<Customer> customers = customersFile.getCustomers();

如果你真的需要一个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);
}

<强>更新

以下是我在项目中使用的依赖项:

<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>

答案 3 :(得分:0)

我在JSON中添加注释时遇到了相同的错误。 我在https://github.com/elastic/elasticsearch/issues/1394

上找到了解决方案
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
final HashMap<String, Customer> listCustomer = mapper.readValue(pathCustomerFile, HashMap.class); //This line gives me no error

答案 4 :(得分:0)

我遇到了同样的问题。我错误地在我的请求有效负载中发送了像“//”这样的评论。 删除“//”对我有用。