是否有任何在线工具或任何其他方式来设置与Java兼容的特定字符串?

时间:2014-03-17 11:57:35

标签: java regex string

我不是很有经验的程序员,我正在研究字符串。我面临的问题是通过在它之前输入'\'来设置每个双引号(使其与java编译器兼容)。例如:

我正在使用GSON库来获取如下字符串:

String example = "[{"salesChannelId":1,"
            + ""companyName":"Prüftechnik Condition Monitoring","
            + ""country":"DE","
            + ""isActive":true,"
            + ""salesChannelContact":[{"salesChannelContactId":40,"
            + ""name":"Hans Muller","
            + ""isActive":true},"
            + "{"salesChannelContactId":41,"
            + ""name":"Peter Pan","
            + ""isActive":true},"}]";

我将每个这样的字符串转换为等价的反斜杠字符串:

String example = "[{\"salesChannelId\":1,"
            + "\"companyName\":\"Prüftechnik Condition Monitoring\","
            + "\"country\":\"DE\","
            + "\"isActive\":true,"
            + "\"salesChannelContact\":[{\"salesChannelContactId\":40,"
            + "\"name\":\"Hans Muller\","
            + "\"isActive\":true},"
            + "{\"salesChannelContactId\":41,"
            + "\"name\":\"Peter Pan\","
            + "\"isActive\":true},\"}]";

现在的问题是我有很多字符串而且非常冗长,我不想浪费时间解析它们或为它们编写单独的代码。是否有任何在线工具或任何捷径可以摆脱它?

1 个答案:

答案 0 :(得分:2)

由于您使用的是Gson,请使用Gson:

new JsonPrimitive(theOriginalJsonAsString).toString()

这将创建一个JSON字符串。