gpt4 book ai didi

java - EclipseLink MOXy JSON 绑定(bind),反斜杠 '\' 转义问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:46 25 4
gpt4 key购买 nike

我正在尝试使用 EclipseLink MOXy(JSON 绑定(bind))将 JSON 字符串转换为 Java 对象。当 JSON 字符串包含“\”字符时,出现以下异常。下面是我正在尝试做的示例程序。我应该如何转义反斜杠,以便将 TestBean 中的名称属性填充为“A\B”?

import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
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());
}
}

class TestBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Exception:
line 1:10 no viable alternative at character '\'
line 1:11 no viable alternative at character 'B'
line 1:14 mismatched character '<EOF>' expecting '"'
line 0:-1 no viable alternative at input '<EOF>'
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: NoViableAltException(-1@[])]
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:980)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:303)
at org.gs1us.glnreg.util.usps.JSONMoxyTest.main(JSONMoxyTest.java:17)
Caused by: Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: NoViableAltException(-1@[])
at org.eclipse.persistence.exceptions.XMLMarshalException.unmarshalException(XMLMarshalException.java:113)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.convertSAXException(SAXUnmarshaller.java:996)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:984)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:425)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:635)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:703)
at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:655)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:301)
... 1 more
Caused by: NoViableAltException(-1@[])
at org.eclipse.persistence.internal.oxm.record.json.JSONParser.value(JSONParser.java:673)
at org.eclipse.persistence.internal.oxm.record.json.JSONParser.pair(JSONParser.java:372)
at org.eclipse.persistence.internal.oxm.record.json.JSONParser.object(JSONParser.java:224)
at org.eclipse.persistence.internal.oxm.record.json.JSONParser.message(JSONParser.java:127)
at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:124)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:972)
... 6 more

我正在使用这些 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

关于java - EclipseLink MOXy JSON 绑定(bind),反斜杠 '\' 转义问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18990895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com