只想问一下转换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的新手,我不知道使用这些表达式。
谢谢。答案 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"
}
}
}
}
}
]
}
}