简体   繁体   English

如何在Java中漂亮地打印无效/不完整的JSON

[英]How to pretty print invalid/incomplete JSON in Java

Assuming an incomplete JSON string (cut off in the middle for eg) how can I best pretty print the characters I have? 假设一个不完整的JSON字符串(例如,在中间被截断),我怎样才能最好地打印出我拥有的字符? All pretty printing I've come across for JSON involves libraries, but they assume a valid and complete JSON structure. 我为JSON遇到的所有漂亮打印都涉及到库,但是它们假定一个有效且完整的JSON结构。

I'm fine with using a "brute-force" method as described here for XML: How to pretty print XML from Java? 我对XML使用这里介绍的“强力”方法很好如何从Java漂亮地打印XML?

Any ideas for similar handling for JSON? 对JSON进行类似处理有什么想法吗?

(I don't want to parse the entire JSON structure since it can be massive, I only want a small part of it printed for logging purposes which I want to be minimal and fast-- and then I want it pretty printed). (我不想解析整个JSON结构,因为它可能很庞大,我只希望将其一小部分打印出来以用于日志记录目的,而我希望它最小且快速-然后我希望将其漂亮地打印出来)。

This is how I ended up solving this. 这就是我最终解决此问题的方式。 Not a great solution, but not sure how it could've been done better (given the problematic input restrictions). 这不是一个很好的解决方案,但是不确定是否可以做得更好(考虑到输入限制问题)。 It's working well however, and attempts to pretty-print json the best it can. 但是,它运行良好,并尝试以最佳方式漂亮地打印json。 It's building on the solution seen here for PHP: https://gist.github.com/GloryFish/1045396 它基于此处为PHP提供的解决方案: https//gist.github.com/GloryFish/1045396

public String prettyPrintJSONAsString(String jsonString) {

    int tabCount = 0;
    StringBuffer prettyPrintJson = new StringBuffer();
    String lineSeparator = "\r\n";
    String tab = "  ";
    boolean ignoreNext = false;
    boolean inQuote = false;

    char character;

    /* Loop through each character to style the output */
    for (int i = 0; i < jsonString.length(); i++) {

        character = jsonString.charAt(i);

        if (inQuote) {

            if (ignoreNext) {
                ignoreNext = false;
            } else if (character == '"') {
                inQuote = !inQuote;
            }
            prettyPrintJson.append(character);
        } else {

            if (ignoreNext ? ignoreNext = !ignoreNext : ignoreNext);

            switch (character) {

            case '[':
                ++tabCount;
                prettyPrintJson.append(character);
                prettyPrintJson.append(lineSeparator);
                printIndent(tabCount, prettyPrintJson, tab);
                break;

            case ']':
                --tabCount;
                prettyPrintJson.append(lineSeparator);
                printIndent(tabCount, prettyPrintJson, tab);
                prettyPrintJson.append(character);
                break;

            case '{':
                ++tabCount;
                prettyPrintJson.append(character);
                prettyPrintJson.append(lineSeparator);
                printIndent(tabCount, prettyPrintJson, tab);
                break;

            case '}':
                --tabCount;
                prettyPrintJson.append(lineSeparator);
                printIndent(tabCount, prettyPrintJson, tab);
                prettyPrintJson.append(character);
                break;

            case '"':
                inQuote = !inQuote;
                prettyPrintJson.append(character);
                break;

            case ',':
                prettyPrintJson.append(character);
                prettyPrintJson.append(lineSeparator);
                printIndent(tabCount, prettyPrintJson, tab);
                break;

            case ':':
                prettyPrintJson.append(character + " ");
                break;

            case '\\':
                prettyPrintJson.append(character);
                ignoreNext = true;
                break;

            default:
                prettyPrintJson.append(character);
                break;
            }
        }
    }

    return prettyPrintJson.toString();
}

private void printIndent(int count, StringBuffer stringBuffer, String indent) {
    for (int i = 0; i < count; i++) {
        stringBuffer.append(indent);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM