在java中,如何解析字符串以在大括号/方括号后添加换行符和制表符
只想问一下转换json字符串的最佳方法是什么:在java中,如何解析字符串以在大括号/方括号后添加换行符和制表符,java,json,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
{"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新手,我不知道如何使用这些表达式
多谢各位
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(obj);
System.out.println(json);
多亏了这个问题似乎解决了我的问题。 请参考链接
也许您需要使用该JSONArray和JSONObject。这是如何使用库格式化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"
}
}
}
}
}
]
}
}
在这种情况下使用正则表达式不是一个好主意。要做到这一点没有简单的方法。你必须用一种很难的方法来做。也许使用外部工具。记事本++有很好的插件(JSTool)。只需在JSon数据上使用JSFormat即可。实际上,我需要将字符串保存在文件中,这样我就无法使用NotePad++来执行此操作。当我打开文件时,应该已经对其进行了编辑谢谢。有可能不使用任何第三方库吗?别提了。对不起,但不幸的是,其他方面我不知道。我向Pshemo了解了您的问题,希望能帮助您。看到这个屏幕截图(json片段100%正确):谢谢。如果我不使用gson库或任何第三方库怎么办?只是org.json?不要让你的生活(以及维护你的代码的人)变得更艰难。尽可能多地使用大家都知道的库,比如GSon、Jackson。不要重新发明轮子。我尝试使用gson,但它在输出字符串中添加了参数“map”。示例:{“map”:{“PrintingPaperStatus”:{“map”:{“Plex”:“Duplex”,“PrintingPaperState”:“PaperFeed”,“jobdattributes”:{“map”:{“JobLogJobId”:“685”,“JobRequestUser”:{“map”:{“UserAttributes”:{“map”:{这只是json字符串的一个片段,因为它太简单了long@user3388797您的输入看起来怎么样?您能将其发布到并共享链接吗?这个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"
}
}
}
}
}
]
}
}