【问题标题】:JSON to Multiline Java String Converter [closed]JSON到多行Java字符串转换器[关闭]
【发布时间】:2019-09-16 13:06:18
【问题描述】:

这个伟大的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  '  + 
 '               }  '  + 
 '           ],  '  + 

【问题讨论】:

    标签: java json string multilinestring jsonconverter


    【解决方案1】:

    您可以在 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" +
                "]"
    

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 2014-01-08
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      相关资源
      最近更新 更多