杰克逊 - 递归解析为Map <string,object =“”> </string,>

时间:2012-12-17 14:32:08

标签: java json parsing jackson

我正在尝试简化我的代码:我想存储键和值(所有字符串)。

我实际上是使用Map<String, Object>来存储它。帽子方式Object可以是值(String)或新节点(Map<String, Object>)。

我该如何简化此代码? 递归函数会很好。

try {
    JsonParser jsonParser = new JsonFactory().createJsonParser(content);

    jsonParser.nextToken();
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        jsonParser.nextToken();

        if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                String key = jsonParser.getCurrentName();
                jsonParser.nextToken();

                if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
                    mData.put(key, new HashMap<String, Object>());
                    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                        String subkey = jsonParser.getCurrentName();
                        jsonParser.nextToken();

                        if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
                            Map<String, Object> subdata = (Map<String, Object>) mData.get(key);
                            subdata.put(subkey, new HashMap<String, Object>());
                            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                                String subsubkey = jsonParser.getCurrentName();
                                jsonParser.nextToken();
                                Map<String, Object> subsubdata = (Map<String, Object>) subdata.get(subkey);
                                LogHelper.d("data[" + key + "][" + subkey + "][" + subsubkey + "]=" + jsonParser.getText());
                                subsubdata.put(subsubkey, jsonParser.getText());
                            }
                        }
                        else {
                            LogHelper.d("data[" + key + "]=" + jsonParser.getText());
                            mData.put(key, jsonParser.getText());
                        }
                    }
                }
                else {
                    LogHelper.d("data[" + key + "]=" + jsonParser.getText());
                    mData.put(key, jsonParser.getText());
                }
            }
        }
        else {
            LogHelper.d("status=" + jsonParser.getText());
            mStatus = jsonParser.getText();
        }
    }
}
catch (IllegalArgumentException e) {
    error("0", "IllegalArgumentException: " + e.getMessage());
}
catch (JsonParseException e) {
    error("0", "IOException: " + e.getMessage());
}
catch (IOException e) {
    error("0", "IOException: " + e.getMessage());
}

1 个答案:

答案 0 :(得分:26)

假设您的最终目标只是将JSON反序列化为Map<String, Object>,那么与Jackson合作的方法就更简单了。使用ObjectMapper

final String json = "{}";
final ObjectMapper mapper = new ObjectMapper();
final MapType type = mapper.getTypeFactory().constructMapType(
    Map.class, String.class, Object.class);
final Map<String, Object> data = mapper.readValue(json, type);

您将需要错误处理等,但这是一个很好的起点。