如何解析字符串以在java中的大括号/方括号后添加换行符和制表符

时间:2014-03-08 09:35:42

标签: java json

只想问一下转换json字符串的最佳方法是什么:

{"scenario":{"title":"User1_PrivatePrint_1Pg_Duplex_BW_A4","description":"Private Print 1 Page Duplex B/W A4 Job","sequences":[{"spiMethod":"notifyAuthenticationStatus","parameter":{"AuthenticationStatus":{"AuthenticationState":"Authenticated","AuthenticationAttributes":{"UserAttributes":{"UserId":"user1","CustomAttribute":"Role1","UserType":"Co"}}}}},{"spiMethod":"validateJobCreation","parameter":{"JobCreationConditions":{"JobAttributes":{"StoredDocumentAttributes":{"UserJobType":"PrivateSpool"},"UserJobType":"PrivatePrint","JobAttributesInPrinting":{"ColorMode":"BW"}}}}}]}}

到这一个:

{
"scenario":{
    "title":"User1_PrivatePrint_1Pg_Duplex_BW_A4",
    "description":"Private Print 1 Page Duplex B/W A4 Job",
    "sequences":[
        {
            "spiMethod":"notifyAuthenticationStatus",
            "parameter":{
                "AuthenticationStatus":{
                    "AuthenticationState":"Authenticated",
                    "AuthenticationAttributes":{
                        "UserAttributes":{
                            "UserId":"user1",
                            "CustomAttribute":"Role1",
                            "UserType":"Co"
                        }                            
                    }                        
                }                    
            } 
        },
        {
            "spiMethod":"validateJobCreation",
            "parameter":{
                "JobCreationConditions":{
                    "JobAttributes":{
                        "StoredDocumentAttributes":{
                            "UserJobType":"PrivateSpool"
                        },
                        "UserJobType":"PrivatePrint",
                        "JobAttributesInPrinting":{
                            "ColorMode":"BW"
                        }                            
                    }                        
                }                    
            }                
        }
    ]
}

}

我在网上搜索,发现我可以使用正则表达式,但由于我是java的新手,我不知道使用这些表达式。

谢谢。

3 个答案:

答案 0 :(得分:1)

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(obj);
System.out.println(json);

感谢mkyong

答案 1 :(得分:0)

这个问题似乎是我的问题。 请参阅链接。

How you can convert JsonArray from String of JsonArray format in Java?

也许你需要使用那个JSONArray和JSONObject。

答案 2 :(得分:0)

这是使用GSon

格式化JSon字符串的方法
String data = "{\"scenario\":{\"title\":\"User1_PrivatePrint_1Pg_Duplex_BW_A4\",\"description\":\"Private Print 1 Page Duplex B/W A4 Job\",\"sequences\":[{\"spiMethod\":\"notifyAuthenticationStatus\",\"parameter\":{\"AuthenticationStatus\":{\"AuthenticationState\":\"Authenticated\",\"AuthenticationAttributes\":{\"UserAttributes\":{\"UserId\":\"user1\",\"CustomAttribute\":\"Role1\",\"UserType\":\"Co\"}}}}},{\"spiMethod\":\"validateJobCreation\",\"parameter\":{\"JobCreationConditions\":{\"JobAttributes\":{\"StoredDocumentAttributes\":{\"UserJobType\":\"PrivateSpool\"},\"UserJobType\":\"PrivatePrint\",\"JobAttributesInPrinting\":{\"ColorMode\":\"BW\"}}}}}]}}";

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser parser = new JsonParser();

JsonElement element = parser.parse(data);
String formattedJSon = gson.toJson(element);
System.out.println(formattedJSon);

输出:

{
  "scenario": {
    "title": "User1_PrivatePrint_1Pg_Duplex_BW_A4",
    "description": "Private Print 1 Page Duplex B/W A4 Job",
    "sequences": [
      {
        "spiMethod": "notifyAuthenticationStatus",
        "parameter": {
          "AuthenticationStatus": {
            "AuthenticationState": "Authenticated",
            "AuthenticationAttributes": {
              "UserAttributes": {
                "UserId": "user1",
                "CustomAttribute": "Role1",
                "UserType": "Co"
              }
            }
          }
        }
      },
      {
        "spiMethod": "validateJobCreation",
        "parameter": {
          "JobCreationConditions": {
            "JobAttributes": {
              "StoredDocumentAttributes": {
                "UserJobType": "PrivateSpool"
              },
              "UserJobType": "PrivatePrint",
              "JobAttributesInPrinting": {
                "ColorMode": "BW"
              }
            }
          }
        }
      }
    ]
  }
}