首页 > 解决方案 > JSON 到多行 Java 字符串转换器

问题描述

这个很棒的codebeautify几乎有我需要的东西,但不会转义特殊字符并使用单引号。对 JavaScript 来说似乎没问题。

有什么工具可以将 JSON 有效负载 a 转换为多行转义的 Java 字符串?

我想制作这样的东西:

"{\r\n" +
    "\"AttributeLogicalName\": \"entityLogicalname\",\r\n" +
    "\"EntityLogicalName\": \"entity\",\r\n" +
    "\"Value\": \"100000002\",\r\n" +
    "\"Label\": {\r\n" +
        "\"LocalizedLabels\": [\r\n" +
            "{\r\n" +
                "\"Label\": \"nerd\",\r\n" +
                "\"LanguageCode\": 1033,\r\n" +
                "\"IsManaged\": false,\r\n" +
                "\"MetadataId\": \"881daca2-5c68-e911-a825-000d3a1d501d\",\r\n" +
                "\"HasChanged\": null\r\n" +
            "}\r\n" +
        "],\r\n" +

但是codebeautify只会产生这个。

 '   {  '  + 
 '       "AttributeLogicalName": "new_localoptionsettoform",  '  + 
 '       "EntityLogicalName": "cr965_testcdsentity",  '  + 
 '       "Value": "100000002",  '  + 
 '       "Label": {  '  + 
 '           "LocalizedLabels": [  '  + 
 '               {  '  + 
 '                   "Label": "nerd",  '  + 
 '                   "LanguageCode": 1033,  '  + 
 '                   "IsManaged": false,  '  + 
 '                   "MetadataId": "881daca2-5c68-e911-a825-000d3a1d501d",  '  + 
 '                   "HasChanged": null  '  + 
 '               }  '  + 
 '           ],  '  + 

标签: javajsonstringmultilinestringjsonconverter

解决方案


您可以在 IntelliJ IDEA 中执行此操作。

将您的 JSON 复制到格式化程序以对其进行格式化,使其显示为漂亮的打印而不是单行:

[
  {
    "_id": "5cc3f7e46a4fe0f0fa9d4084",
    "index": 0,
    "guid": "1d431fc7-8ec1-477b-8a22-d21f2169a474",
    "isActive": true,
    "age": 29,
    "name": "Dixon Downs",
    "gender": "male",uino, Louisiana, 4533",
    "about": "Ut nostrud consectetur eiusmod est eiusmod sit commodo nulla minim magna. Anim esse fugiat et quis ullamco aliquip enim. Excepteur laboris laboris proident elit aliqua ullamco quis ut reprehenderit et aliquip dolore id labore. Mollit officia quis ullamco mollit. Veniam laborum ex elit ut veniam sunt ullamco ad cupidatat ullamco in. Occaecat in irure excepteur elit pariatur ex ex elit adipisicing occaecat minim.\r\n",
    "tags": [
      "laborum",
      "consectetur",
      "occaecat",
      "duis",
      "dolore",
      "sit",
      "aute"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Owen Kinney"
      },
      {
        "id": 1,
        "name": "Wiggins Marks"
      },
      {
        "id": 2,
        "name": "Maura Lara"
      }
    ]
  }
]

然后,在 IntelliJ IDEA 中编写一个字符串文字:

""

并将光标放在两个引号之间,然后粘贴。你会看到的:

    "[\n" +
            "  {\n" +
            "    \"_id\": \"5cc3f7e46a4fe0f0fa9d4084\",\n" +
            "    \"index\": 0,\n" +
            "    \"guid\": \"1d431fc7-8ec1-477b-8a22-d21f2169a474\",\n" +
            "    \"isActive\": true,\n" +
            "    \"age\": 29,\n" +
            "    \"name\": \"Dixon Downs\",\n" +
            "    \"gender\": \"male\",uino, Louisiana, 4533\",\n" +
            "    \"about\": \"Ut nostrud consectetur eiusmod est eiusmod sit commodo nulla minim magna. Anim esse fugiat et quis ullamco aliquip enim. Excepteur laboris laboris proident elit aliqua ullamco quis ut reprehenderit et aliquip dolore id labore. Mollit officia quis ullamco mollit. Veniam laborum ex elit ut veniam sunt ullamco ad cupidatat ullamco in. Occaecat in irure excepteur elit pariatur ex ex elit adipisicing occaecat minim.\\r\\n\",\n" +
            "    \"tags\": [\n" +
            "      \"laborum\",\n" +
            "      \"consectetur\",\n" +
            "      \"occaecat\",\n" +
            "      \"duis\",\n" +
            "      \"dolore\",\n" +
            "      \"sit\",\n" +
            "      \"aute\"\n" +
            "    ],\n" +
            "    \"friends\": [\n" +
            "      {\n" +
            "        \"id\": 0,\n" +
            "        \"name\": \"Owen Kinney\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"id\": 1,\n" +
            "        \"name\": \"Wiggins Marks\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"id\": 2,\n" +
            "        \"name\": \"Maura Lara\"\n" +
            "      }\n" +
            "    ]\n" +
            "  }\n" +
            "]"

推荐阅读