Spring逃脱已经逃脱了字符串

时间:2016-12-06 09:28:39

标签: java json spring jackson

所以我将这个字符串保存在数据库中:

{\"facebook\":\"fb.com\",\"twitter\":\"twitter.com\",\"instagram\":\"\",\"googlePlus\":\"\",\"others\":\"espn.com\"}

但是当我调用GET api时,我在JSON中得到了这个

{\\\"facebook\\\":\\\"fb.com\\\",\\\"twitter\\\":\\\"twitter.com\\\",\\\"instagram\\\":\\\"\\\",\\\"googlePlus\\\":\\\"\\\",\\\"others\\\":\\\"espn.com\\\"}

为什么会发生这种情况?如何获得存储在数据库中的完全相同的数据?

3 个答案:

答案 0 :(得分:6)

当您检索数据时,它会再次转义,因为Spring认为\字符是数据的一部分而不是用于转义"

您永远不想存储转义字符(无论是JSON特殊字符,文本中的HTML字符,......),您必须存储未转义的数据以解决您的问题。在显示数据时必须进行转义,而不是在存储数据时进行转义。

由于您遇到的问题,存储转义数据是一种不好的做法,但也因为它会占用数据库中无用的存储空间(这对您来说可能不是一个问题,但会与数百万行。

答案 1 :(得分:1)

您还可以在<?xml version="1.0" encoding="UTF-8"?> <!-- config.xml reference: https://build.phonegap.com/docs/config-xml --> <widget xmlns = "http://www.w3.org/ns/widgets" xmlns:gap = "http://phonegap.com/ns/1.0" id = "MyAppId" version = "3.0.1"> <name>MyApName</name> <description>MyAppDescription</description> <author href="http://MyAppURL" email="MyAppMail@MyAppDomain.example">MyAppAuthor</author> <preference name="permissions" value="none"/> <preference name="orientation" value="default" /> <preference name="target-device" value="universal" /> <preference name="fullscreen" value="true" /> <preference name="webviewbounce" value="true" /> <preference name="prerendered-icon" value="true" /> <preference name="stay-in-webview" value="false" /> <preference name="ios-statusbarstyle" value="white-opaque" /> <preference name="detect-data-types" value="true" /> <preference name="exit-on-suspend" value="false" /> <preference name="show-splash-screen-spinner" value="false" /> <preference name="auto-hide-splash-screen" value="true" /> <preference name="disable-cursor" value="false" /> <preference name="android-installLocation" value="auto" /> <plugin name="cordova-plugin-device" /> <plugin name="cordova-plugin-inappbrowser" /> <plugin name="cordova-plugin-network-information" /> <plugin name="cordova-plugin-splashscreen" /> <icon platform="ios" src="res/icons/ios/icon-small.png" width="29" height="29" /> <icon platform="ios" src="res/icons/ios/icon-small-2x.png" width="58" height="58" /> <icon platform="ios" src="res/icons/ios/icon-40.png" width="40" height="40" /> <icon platform="ios" src="res/icons/ios/icon-40-2x.png" width="80" height="80" /> <icon platform="ios" src="res/icons/ios/icon-50.png" width="50" height="50" /> <icon platform="ios" src="res/icons/ios/icon-50-2x.png" width="100" height="100" /> <icon platform="ios" src="res/icons/ios/icon.png" width="57" height="57" /> <icon platform="ios" src="res/icons/ios/icon-2x.png" width="114" height="114" /> <icon platform="ios" src="res/icons/ios/icon-60.png" width="60" height="60" /> <icon platform="ios" src="res/icons/ios/icon-60-2x.png" width="120" height="120" /> <icon platform="ios" src="res/icons/ios/icon-60-3x.png" width="180" height="180" /> <icon platform="ios" src="res/icons/ios/icon-72.png" width="72" height="72" /> <icon platform="ios" src="res/icons/ios/icon-72-2x.png" width="144" height="144" /> <icon platform="ios" src="res/icons/ios/icon-76.png" width="76" height="76" /> <icon platform="ios" src="res/icons/ios/icon-76-2x.png" width="152" height="152" /> <splash platform="ios" src="res/screens/ios/screen-iphone-portrait.png" width="320" height="480" /> <splash platform="ios" src="res/screens/ios/screen-iphone-portrait-2x.png" width="640" height="960" /> <splash platform="ios" src="res/screens/ios/screen-iphone-portrait-568h-2x.png" width="640" height="1136" /> <splash platform="ios" src="res/screens/ios/screen-iphone-portrait-667h.png" width="750" height="1334" /> <splash platform="ios" src="res/screens/ios/screen-iphone-portrait-736h.png" width="1242" height="2208" /> <splash platform="ios" src="res/screens/ios/screen-ipad-portrait.png" width="768" height="1024" /> <splash platform="ios" src="res/screens/ios/screen-ipad-portrait-2x.png" width="1536" height="2048" /> <icon platform="android" qualifier="ldpi" src="res/icons/android/icon-36-ldpi.png"/> <icon platform="android" qualifier="mdpi" src="res/icons/android/icon-48-mdpi.png"/> <icon platform="android" qualifier="hdpi" src="res/icons/android/icon-72-hdpi.png"/> <icon platform="android" qualifier="xhdpi" src="res/icons/android/icon-96-xhdpi.png"/> <icon platform="android" qualifier="xxhdpi" src="res/icons/android/icon-144-xxhdpi.png"/> <icon platform="android" qualifier="xxxhdpi" src="res/icons/android/icon-192-xxxhdpi.png"/> <splash platform="android" qualifier="ldpi" src="res/screens/android/screen-ldpi-portrait.png" /> <splash platform="android" qualifier="mdpi" src="res/screens/android/screen-mdpi-portrait.png" /> <splash platform="android" qualifier="hdpi" src="res/screens/android/screen-hdpi-portrait.png" /> <splash platform="android" qualifier="xhdpi" src="res/screens/android/screen-xhdpi-portrait.png" /> <icon platform="wp8" src="res/icons/wp8/ApplicationIcon.png" width="99" height="99" /> <icon platform="wp8" src="res/icons/wp8/Background.png" width="159" height="159" /> <icon platform="windows" src="res/icons/windows8/logo.png" width="150" height="150" /> <icon platform="windows" src="res/icons/windows8/smalllogo.png" width="30" height="30" /> <icon platform="windows" src="res/icons/windows8/storelogo.png" width="50" height="50" /> <gap:config-file platform="ios" parent="CFBundleShortVersionString"> <string>3.0.1</string> </gap:config-file> <access origin="*"/> <plugin name="cordova-plugin-whitelist" version="1" /> <allow-intent href="http://*/*" /> <allow-intent href="https://*/*" /> <allow-intent href="tel:*" /> <allow-intent href="sms:*" /> <allow-intent href="mailto:*" /> <allow-intent href="geo:*" /> <platform name="android"> <allow-intent href="market:*" /> </platform> <platform name="ios"> <allow-intent href="itms:*" /> <allow-intent href="itms-apps:*" /> </platform> 机制中使用apache构建Json数据。请参阅参考https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/StringEscapeUtils.html

答案 2 :(得分:0)

获取数据后,将其保存在String中,然后:

 String newJava = str.replace("\\\", "\");