Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java EclipseLink MOXy JSON绑定,反斜杠'\';逃逸问题_Java_Json_Eclipselink_Unmarshalling_Moxy - Fatal编程技术网

Java EclipseLink MOXy JSON绑定,反斜杠'\';逃逸问题

Java EclipseLink MOXy JSON绑定,反斜杠'\';逃逸问题,java,json,eclipselink,unmarshalling,moxy,Java,Json,Eclipselink,Unmarshalling,Moxy,我正在尝试使用EclipseLink MOXy(JSON绑定)将JSON字符串转换为Java对象。当JSON字符串包含“\”字符时,我得到以下异常。下面是我正在尝试做的一个示例程序。我应该做什么来转义反斜杠,以便TestBean中的name属性填充为“A\B” 导入java.io.StringReader; 导入javax.xml.bind.JAXBContext; 导入javax.xml.bind.JAXBException; 导入javax.xml.bind.Unmarshaller; 导入

我正在尝试使用EclipseLink MOXy(JSON绑定)将JSON字符串转换为Java对象。当JSON字符串包含“\”字符时,我得到以下异常。下面是我正在尝试做的一个示例程序。我应该做什么来转义反斜杠,以便TestBean中的name属性填充为“A\B”

导入java.io.StringReader;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Unmarshaller;
导入javax.xml.transform.stream.StreamSource;
导入org.eclipse.persistence.jaxb.UnmarshallerProperties;
公共类JSONMoxyTest{
公共静态void main(字符串[]args)抛出JAXBEException{
Unmarshaller Unmarshaller=JAXBContext.newInstance(TestBean.class).createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_类型,“application/json”);
setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT,false);
字符串jsonStr=“{\'name\':\'A\\B\'}”/{“name”:“A\B”}
TestBean bean=unmarshaller.unmarshal(新的StreamSource(新的StringReader(jsonStr)),TestBean.class.getValue();
System.out.println(bean.getName());
}
}
类TestBean{
私有字符串名称;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
例外情况:
第1行:10字符“\”处没有可行的替代方案
第1行:11字符“B”处没有可行的替代方案
第1行:14不匹配的字符“”应为“”
第0行:-1输入“”处没有可行的替代方案
线程“main”javax.xml.bind.UnmarshaleException中出现异常
-除此之外:
[Exception[EclipseLink-25004](Eclipse持久性服务-2.5.0.v20130507-3faac2b):org.Eclipse.Persistence.exceptions.xmlMarshallException
异常说明:解组文档时出错
内部异常:NoViableAltException(-1@[])]
位于org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:980)
位于org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:303)
位于org.gs1us.glnreg.util.usps.JSONMoxyTest.main(JSONMoxyTest.java:17)
原因:异常[EclipseLink-25004](Eclipse持久性服务-2.5.0.v20130507-3faac2b):org.Eclipse.Persistence.exceptions.xmlmarshallexception
异常说明:解组文档时出错
内部异常:NoviableException(-1@[])
位于org.eclipse.persistence.exceptions.xmlmarshaleexception.unmarshaleexception(xmlmarshaleexception.java:113)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.convertSAXException(SAXUnmarshaller.java:996)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:984)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:425)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:635)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:703)
位于org.eclipse.persistence.oxm.xmlumarshaller.unmarshal(xmlumarshaller.java:655)
位于org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:301)
…还有一个
由以下原因引起:NoviableException(-1@[]
位于org.eclipse.persistence.internal.oxm.record.json.JSONParser.value(JSONParser.java:673)
位于org.eclipse.persistence.internal.oxm.record.json.JSONParser.pair(JSONParser.java:372)
位于org.eclipse.persistence.internal.oxm.record.json.JSONParser.object(JSONParser.java:224)
位于org.eclipse.persistence.internal.oxm.record.json.JSONParser.message(JSONParser.java:127)
位于org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:124)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:972)
…还有6个

我正在使用这些jar:org.eclipse.persistence.antlr-2.5.0.jar、org.eclipse.persistence.asm-2.5.0.jar、org.eclipse.persistence.core-2.5.0.jar、org.eclipse.persistence.moxy-2.5.0.jar

在JSON中,字符必须转义为
\

已更正
JSONMoxyTest

import java.io.StringReader;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;

public class JSONMoxyTest {

    public static void main(String[] args) throws JAXBException {
        Unmarshaller unmarshaller = JAXBContext.newInstance(TestBean.class).createUnmarshaller();
        unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
        unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
        String jsonStr = "{\"name\":\"A\\\\B\"}"; //{"name":"A\\B"}
        TestBean bean = unmarshaller.unmarshal(new StreamSource(new StringReader(jsonStr )), TestBean.class).getValue();
        System.out.println(bean.getName());
    }

}
输出

A\B
A\B