Jackson ObjectMapper配置 - 错误JsonParseException:意外字符('b'(代码98)):

时间:2014-12-17 02:49:14

标签: java json jackson

我已按以下格式生成JSON

[{"empNo":"2390","empName":"JAMES","projects":{"projectId":209,"projectName":"Z560"}}]

如何为上述配置ObjectMapper?

我已将ObjectMapper声明为

private static final ObjectMapper om = new ObjectMapper();
static {
    om.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
    om.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    om.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, 
                 true);
    om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, 
                 false);
    om.getSerializationConfig().setSerializationInclusion
   (JsonSerialize.Inclusion.NON_NULL);
   }

但是我仍然收到以下错误

  

com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException       严重:MappableContainerException中包含的异常不能       被映射到响应,重新投掷到HTTP容器       org.codehaus.jackson.JsonParseException:意外的字符(' b'(代码98)):       期望一个有效的值(数字,字符串,数组,对象,       ' true',' false'或者' null')在[来源:java.io.StringReader@1fef0b44;行:1,       专栏:2]

预期输出

{"empNo":"2390","empName":"JAMES","projectId":"209","projectName":"Z560"}

1 个答案:

答案 0 :(得分:-1)

有点冗长,可以优化。请参阅this了解更多信息。

public static void main(String[] args) throws IOException {
        String originalJson = "{\"empNo\":\"2390\",\"empName\":\"JAMES\",\"projects\":{\"projectId\":209,\"projectName\":\"Z560\"}}";
        try {
            JSONObject jsonObject = new JSONObject(originalJson);
            Map<String, Object> map = getMap(jsonObject);
            System.out.println("My Old Map => " + map);

            Map<String, Object> newMap = new HashMap<String, Object>();

            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (entry.getKey().equals("projects")) {
                    Map<String, Object> projectMap = (Map<String, Object>) entry.getValue();
                    for (Map.Entry<String, Object> entry1 : projectMap.entrySet()) {
                        newMap.put(entry1.getKey(), entry1.getValue());
                    }
                } else {
                    newMap.put(entry.getKey(), entry.getValue().toString());
                }
            }

            JSONObject jsonObject1 = new JSONObject(newMap);
            System.out.println("My New Map => " + newMap);
            System.out.println("Expected Json String => " + jsonObject1.toString());

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static Map getMap(JSONObject object) {
        Map<String, Object> map = new HashMap<String, Object>();

        Object jsonObject = null;
        String key = null;
        Object value = null;

        try {
            Iterator<String> keys = object.keys();
            while (keys.hasNext()) {

                key = null;
                value = null;

                key = keys.next();

                if (null != key && !object.isNull(key)) {
                    value = object.get(key);
                }

                if (value instanceof JSONObject) {
                    map.put(key, getMap((JSONObject) value));
                    continue;
                }

                if (value instanceof JSONArray) {
                    JSONArray array = ((JSONArray) value);
                    List list = new ArrayList();
                    for (int i = 0 ; i < array.length() ; i++) {
                        jsonObject = array.get(i);
                        if (jsonObject instanceof JSONObject) {
                            list.add(getMap((JSONObject) jsonObject));
                        } else {
                            list.add(jsonObject);
                        }
                    }
                    map.put(key, list);
                    continue;
                }

                map.put(key, value);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return map;
    }

<强>输出

  

我的旧地图=&gt; {projects = {projectId = 209,projectName = Z560},   empName = JAMES,empNo = 2390}

     

我的新地图=&gt; {empName = JAMES,empNo = 2390,projectId = 209,   PROJECTNAME = Z560}

     

预期的Json String =&gt;   { “empName”: “JAMES”, “EMPNO”: “2390”, “专案编号”:209, “项目名”: “Z560”}