简体   繁体   English

将纯文本转换为 Json - Java - Spring Boot

[英]Transform plain text into Json - Java - Spring Boot

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.我正在尝试将文本字符串str = {Message={description=Data Loading Success}}转换为 Json 格式,因此我可以使用 Gson 将生成的 json 转换为 Java 类。

So far I used String formattedResponse = str.replace("=", ":");到目前为止,我使用了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"}}并得到{Message:{description:Data Loading Success}}但我不知道如何添加"并最终得到{"Message": {"description": "Data Loading Success"}}

        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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM