将包含特殊字符的jsonString转换为jsonObject

fdbelqdn  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(98)

我有jsonString包含特殊字符。我想删除这些特殊字符以获得正确的jsonString然后jsonObject。我的代码是:

String text="\"\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"createdAt\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"2023-10-31T11:43:29\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"updatedAt\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"2023-10-31T11:43:30\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"createdBy\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":null,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\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\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"priority\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n}\\\\\\\"\\\"\"";

字符串
你知道如何将不正确的字符串转换为正确的jsonObject吗?
我试过替换所有的方法,但我仍然看到一些字符:(
到目前为止我所做的:

text.replace("\\","").replace("\\n","UUU").replace("\\n",TTT").replace("\"\\\"\\\"\\\"\\\"{","\"{")

vxf3dgd4

vxf3dgd41#

写在这里只是为了展示如何分析这些问题并设计解决方案。这里提供的解决方案仅适用于粘贴的特定字符串和任何具有类似额外字符的字符串。
方法:
1.研究字符串以了解包含的模式。
1.创建模式以删除或替换它们以正确的顺序
下面是一个类,它可以为有问题的字符串实现它。我添加了一些注解来解释这种方法。

public class SOCleanupJSONString {
    public static void main(String[] args) {
        String text="\"\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"createdAt\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"2023-10-31T11:43:29\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"updatedAt\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"hello world\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"status\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"CREATED\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"priority\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n}\\\\\\\"\\\"\"";

        /* 1. Extract the JSON block from the string. 
           NOTE: This pattern is only for object blocks. You may have array blocks as well. Please change this pattern for that. */
        Pattern JSON_BLOCK = Pattern.compile( "\\{.*}" );
        Matcher m = JSON_BLOCK.matcher( text );
        if( m.find() ) {
            text = m.group();
            System.out.println(text);

            /* 2. Un-escape quotes */
            text = text.replaceAll("\\\"", "\"");

            /* 3. Remove (JSON escaped) newline text */
            text = text.replaceAll("\\\\n", "" );

            /* 4. Remove all extra double-slashes.
             *    NOTE: This can be erroneous, if the pattern appears within a value. */
            text = text.replaceAll("\\\\", "" );
            System.out.println(text);
        }
        else System.out.println( "No JSON block found" );

        parse( text );

    }

    private static void parse( String text ){
        ObjectMapper om = new ObjectMapper();
        try {
            Map map = om.readValue( text, Map.class );
            System.out.println( map );
        }
        catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }
}

字符串
结果JSON字符串:

{"createdAt":"2023-10-31T11:43:29","updatedAt":"2023-10-31T11:43:30","createdBy":null,"updatedBy":null,"isDeleted":false,"messageValue":{"name":"hello world","status":"CREATED","priority":1}}


使用Jackson将上述内容解组到Map示例后的Map.toString()输出:

{createdAt=2023-10-31T11:43:29, updatedAt=2023-10-31T11:43:30, createdBy=null, updatedBy=null, isDeleted=false, messageValue={name=hello world, status=CREATED, priority=1}}

相关问题