How to escape Special Characters in JSON

前端 未结 2 1363
情深已故
情深已故 2021-01-12 00:01

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 tha

相关标签:
2条回答
  • 2021-01-12 00:18

    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...

    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.

    Here's an example that you can use in MEL. String.valueOf is necessary because quoteAsString returns 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>
    
    0 讨论(0)
  • 2021-01-12 00:22

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

    Clean it by running :

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

    Convert it back to JSON data using any JSON library :

    JSONObject inputParams = JSONObject.fromObject(input);
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题