我试图使用Jettison解析JSON对象.
这是我正在使用的代码
String s ="{\"appUsage\":[{\"appName\":\"ANDROID\",\"totalUsers\":\"0\"},{\"appName\":\"IOS\",\"totalUsers\":\"4\"}]}";
JSONObject obj = new JSONObject(s);
ArrayList l1 = (ArrayList) jsonParser(ArrayList.class, obj);
public static Object jsonParser(Class c, JSONObject obj)
throws JSONException, XMLStreamException, JAXBException {
JAXBContext jc = JAXBContext.newInstance(c);
Configuration config = new Configuration();
MappedNamespaceConvention con = new MappedNamespaceConvention(config);
XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);
Unmarshaller unmarshaller = jc.createUnmarshaller();
ArrayListcustomer = (ArrayList) unmarshaller.unmarshal(xmlStreamReader);
return customer;
}
我收到了这个错误
Exception in thread “main” javax.xml.bind.UnmarshalException
– with linked exception: [javax.xml.bind.UnmarshalException: unexpected element (uri:””, local:”appUsage”). Expected elements are
(none)] at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown
Source) at com.json.UnmarshalDemo.jsonParser(UnmarshalDemo.java:56)
at com.json.UnmarshalDemo.main(UnmarshalDemo.java:33) Caused by:
javax.xml.bind.UnmarshalException: unexpected element (uri:””,
local:”appUsage”). Expected elements are (none) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(Unknown
Source) at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown
Source) … 4 more Caused by: javax.xml.bind.UnmarshalException:
unexpected element (uri:””, local:”appUsage”). Expected elements are
(none) … 14 more
如何解决这个问题
解决方法:
如果您最终正在寻找使用JAXB(JSR-222)实现与JSON交互的方法,那么以下是使用MOXy完成它的方法. Jettison是一个有趣的库,但是你会遇到一些问题:
演示
仅使用标准Java SE API.需要在Unmarshaller上设置两个MOXy特定属性:“eclipselink.media-type”指定“application / json”,“eclipselink.json.include-root”指示没有根节点.
package forum9924567;
import java.io.StringReader;
import java.util.ArrayList;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
private static final String s ="{\"appUsage\":[{\"appName\":\"ANDROID\",\"totalUsers\":\"0\"},{\"appName\":\"IOS\",\"totalUsers\":\"4\"}]}";
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty("eclipselink.media-type", "application/json");
unmarshaller.setProperty("eclipselink.json.include-root", false);
StreamSource json = new StreamSource(new StringReader(s));
Root root = unmarshaller.unmarshal(json, Root.class).getValue();
ArrayList customer = root.appUsage;
for(MiAppUsage miAppUsage : customer) {
System.out.print(miAppUsage.appName);
System.out.print(' ');
System.out.println(miAppUsage.totalUsers);
}
}
}
根
我不得不介绍这个课程来满足你的用例.如果你的JSON看起来像我们可以消除这个类:[{“appName”:“ANDROID”,“totalUsers”:“0”},{“appName”:“IOS”,“totalUsers”:“4”}].
package forum9924567;
import java.util.ArrayList;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
ArrayList appUsage;
}
MiAppUsage
package forum9924567;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class MiAppUsage {
String appName;
int totalUsers;
}
jaxb.properties
要将MOXy指定为JAXB提供程序,您需要添加一个名为java.properties的文件,并在与您的域类相同的包中添加以下条目:
javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory
产量
以下是运行演示代码的输出:
ANDROID 0
IOS 4
欲获得更多信息
标签:json,java,jettison
来源: https://codeday.me/bug/20190621/1250473.html