将普通文本转换为Json – Java – Spring Boot

huangapple go评论69阅读模式
英文:

Transform plain text into Json - Java - Spring Boot

问题

我正在尝试将文本字符串 str = {Message={description=Data Loading Success}} 转换为 Json 格式,以便我可以使用 Gson 将生成的 json 转换为 Java 类。

到目前为止,我使用了 String formattedResponse = str.replace(""", ":"); 并获得了 {Message:{description:Data Loading Success}},但我不知道如何添加 ",最终获得 {"Message": {"description": "Data Loading Success"}}

英文:

I'm trying to transform a text string str = {Message={description=Data Loading Success}} into a Json format, so I can use Gson to translate the resulting json in a Java class.

So far I used String formattedResponse = str.replace("=", ":"); and got {Message:{description:Data Loading Success}} but I have no idea how to add the " and finally get {"Message": {"description": "Data Loading Success"}}

答案1

得分: 0

        String str = "{Message={description=Data Loading Success}}";
		String result = str.replaceAll("((\\w\\s*)+)", "\"$1\"");
		System.out.println(result);
		str= "TEST:STRING";
		result = str.replaceAll("((\\w\\s*)+)", "\"$1\"");
		System.out.println(result);
英文:
        String str = "{Message={description=Data Loading Success}}";
		String result = str.replaceAll("((\\w\\s*)+)", "\"$1\"");
		System.out.println(result);
		str= "TEST:STRING";
		result = str.replaceAll("((\\w\\s*)+)", "\"$1\"");
		System.out.println(result);

Using this I get the results:

{"Message"={"description"="Data Loading Success"}}
"TEST":"STRING"

huangapple
  • 本文由 发表于 2020年9月11日 03:24:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63836418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定