我正在尝试使用 Jettison 解析 JSON 对象。这是我正在使用的代码
String s ="{\"appUsage\":[{\"appName\":\"ANDROID\",\"totalUsers\":\"0\"},{\"appName\":\"IOS\",\"totalUsers\":\"4\"}]}";
JSONObject obj = new JSONObject(s);
ArrayList<MiAppUsage> l1 = (ArrayList<MiAppUsage>) 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();
ArrayList<MiAppUsage>customer = (ArrayList<MiAppUsage>) unmarshaller.unmarshal(xmlStreamReader);
return customer;
}
我收到此错误
线程“主”javax.xml.bind.UnmarshalException 中的异常 - 带有链接异常:[javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“appUsage”)。预期的元素是(无)] 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl 的 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(未知来源)。 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(未知来源)在 com.json.UnmarshalDemo.jsonParser(UnmarshalDemo.java:56) 在 com.json.UnmarshalDemo .main(UnmarshalDemo.java:33) 引起:javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“appUsage”)。com.sun.xml.internal.bind.v2 中的预期元素是(无)。v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source) ... 4 更多原因:javax.xml.bind .UnmarshalException:意外元素(uri:“”,本地:“appUsage”)。预期的元素是(无)... 14 更多
如何解决这个问题