如何在JSON中转义特殊字符

时间:2022-06-16 00:25:51

We have a form which has a long paragraph for a scienctific application that contains characters like symbol beta(ß-arrestin) etc. We have a JSON service running on Mule that takes the data and persists to an oracle database. This particular element with long paragraph is giving me an error in RAML/JSON. Below is the error

我们有一个表格,它包含一个scienctific应用程序的长段落,其中包含符号beta(ß-arrestin)等字符。我们在Mule上运行一个JSON服务,它接收数据并持久存储到oracle数据库。这个带有长段的特殊元素在RAML / JSON中给出了一个错误。以下是错误

com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 9)): has to be escaped using backslash to be included in string value

The form element to which the scientists write we have no control. So on the Mule side how can we escape these characters automagically like java has URLEncoded. Many Thanks

科学家们写的形式元素我们无法控制。所以在Mule方面,我们如何自动地逃避这些字符,就像java具有URLEncoded一样。非常感谢

2 个解决方案

#1


1  

In your case it looks like the incoming data is malformed. It must be in an encoding supported by the JSON spec: UTF-8 (default), UTF-16, or UTF-32. So not sure if the following is applicable. Nevertheless...

在您的情况下,看起来传入的数据格式不正确。它必须采用JSON规范支持的编码:UTF-8(默认),UTF-16或UTF-32。所以不确定以下内容是否适用。不过...

For most apps I would recommend JSON to Object mapping, which will take care of the escaping. Otherwise, you can call Jackson's (the JSON library used by Mule) String escape method directly.

对于大多数应用程序,我建议使用JSON到Object映射,它将负责转义。否则,您可以直接调用Jackson的(Mule使用的JSON库)字符串转义方法。

Here's an example that you can use in MEL. String.valueOf is necessary because quoteAsString returns char[]:

这是您可以在MEL中使用的示例。 String.valueOf是必需的,因为quoteAsString返回char []:

<configuration>
  <expression-language>
    <import class="org.codehaus.jackson.io.JsonStringEncoder" />
    <global-functions>
      def quoteJSONString(s) {
        String.valueOf(JsonStringEncoder.getInstance().quoteAsString(s))
      }
    </global-functions>
  </expression-language>
</configuration>

#2


2  

At the target you can receive the data as text/plain.

在目标,您可以以text / plain格式接收数据。

Clean it by running :

运行清理它:

input.replaceAll("\\p{Cc}", "").

Convert it back to JSON data using any JSON library :

使用任何JSON库将其转换回JSON数据:

JSONObject inputParams = JSONObject.fromObject(input);

Hope it helps.

希望能帮助到你。

#1


1  

In your case it looks like the incoming data is malformed. It must be in an encoding supported by the JSON spec: UTF-8 (default), UTF-16, or UTF-32. So not sure if the following is applicable. Nevertheless...

在您的情况下,看起来传入的数据格式不正确。它必须采用JSON规范支持的编码:UTF-8(默认),UTF-16或UTF-32。所以不确定以下内容是否适用。不过...

For most apps I would recommend JSON to Object mapping, which will take care of the escaping. Otherwise, you can call Jackson's (the JSON library used by Mule) String escape method directly.

对于大多数应用程序,我建议使用JSON到Object映射,它将负责转义。否则,您可以直接调用Jackson的(Mule使用的JSON库)字符串转义方法。

Here's an example that you can use in MEL. String.valueOf is necessary because quoteAsString returns char[]:

这是您可以在MEL中使用的示例。 String.valueOf是必需的,因为quoteAsString返回char []:

<configuration>
  <expression-language>
    <import class="org.codehaus.jackson.io.JsonStringEncoder" />
    <global-functions>
      def quoteJSONString(s) {
        String.valueOf(JsonStringEncoder.getInstance().quoteAsString(s))
      }
    </global-functions>
  </expression-language>
</configuration>

#2


2  

At the target you can receive the data as text/plain.

在目标,您可以以text / plain格式接收数据。

Clean it by running :

运行清理它:

input.replaceAll("\\p{Cc}", "").

Convert it back to JSON data using any JSON library :

使用任何JSON库将其转换回JSON数据:

JSONObject inputParams = JSONObject.fromObject(input);

Hope it helps.

希望能帮助到你。