Java escape JSON String?

前端 未结 10 653
再見小時候
再見小時候 2020-12-08 11:27

I have the following JSON string that i am sending to a NodeJS server:

String string = \"{\\\"id\\\":\\\"\" + userID + \"\\\",\\\"type\\\":\\\"\" + methoden          


        
10条回答
  •  孤城傲影
    2020-12-08 12:03

    Apache Commons

    If you're already using Apache commons, it provides a static method for this:

    StringEscapeUtils.escapeJson("some string")
    

    It converts any string into one that's properly escaped for inclusion in JSON

    See the documentation here

提交回复
热议问题