【问题标题】:org.codehaus.jackson.JsonParseException: Unexpected character ('/' (code 47))org.codehaus.jackson.JsonParseException:意外字符('/'(代码 47))
【发布时间】:2015-08-28 06:15:46
【问题描述】:

我有一个包含 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:意外字符('/'(代码 47)):可能是(非标准)注释? (由于未为解析器启用功能“ALLOW_COMMENTS”,因此无法识别)

我该怎么做?

【问题讨论】:

  • 可能pathCustomerFile是一个字符串?我有一个类似的问题,我输入参数有误。通过替换为new File (pathCustomerFile) 修复

标签: java json hashmap


【解决方案1】:

我有同样的问题。我错误地在我的请求有效负载中发送了 cmets,例如“//”。 删除“//”对我有用。

【讨论】:

    【解决方案2】:

    我在 JSON 中添加 cmets 时遇到了同样的错误。 我在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
    

    【讨论】:

      【解决方案3】:

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

      (基本上,它是在读取文件路径中的“/”并在上面抛出错误。

      【讨论】:

        【解决方案4】:

        我会为拥有客户列表和 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);
        }
        

        更新

        以下是我在项目的 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>
        

        【讨论】:

        • mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); 错误:找不到合适的配置方法(DeserializationFeature,boolean)
        • 你用的是什么版本的jackson?
        • gson 2.3.1,jackson-mapper-asl 1.9.13
        • @DavideFruci 查看我的更新,也可以尝试不配置映射器以接受单个值作为数组
        • 我添加了你的依赖,但我又遇到了同样的错误。
        【解决方案5】:

        请仔细检查您的输入 JSON 字符串,您是否将其错误地转义或 / 悬空在某处。例如/\"name\"。然后提供正确的类型映射方式:

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

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

        ** 仅使用 HashMap.class 的映射不会给您想要的结果,因为 Jackson 会将您的 JSON 映射到 Map&lt;String, Map&gt;。你会发现,什么时候你会尝试从 Map 中获取一个值并像 Customer 类型一样操作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-03
          • 1970-01-01
          • 2018-07-22
          • 2021-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多