Search code examples
jsonstruts2escapingbackslashstruts2-json-plugin

Struts2 json return extra \ backward slash


My Struts2 is currently returning this:

[{"pathlocation":"http:\/\/103.231.125.106\/nesos\/cartoon\/151020183506download.jpeg"}]

I want this:

[{"pathlocation":"http://103.231.125.106/nesos/cartoon/151026121150download.jpeg"}]
<action name="dayofcartoon" method="dayofcartoon" class="foo.bar.AdminInfoAction">
    <result name="json" type="json"> 
        <param name="excludeNullProperties">true</param>
        <param name="root">
         commissioner_info
        </param>            
     </result>
</action> 

Solution

  • Many JSON encoders escape chars / as \/. It's not incorrect but is not mandatory. It's related with the /> char combination and allow JSON string to be embeded in Javascript code.

    If you are using this JSON with Javascript code, you shouldn't be worried about it because Javascript handles it correctly. Both \/ and / are equivalents.

    If you should return unescaped response from your Struts2 action then try to force to your JSON encoder to not escape the slash char.

    Here you've got a good article talking about this topic: http://andowebsit.es/blog/noteslog.com/post/the-solidus-issue/