gpt4 book ai didi

java - 使用 Jettison 进行 JSON 解析

转载 作者:搜寻专家 更新时间:2023-11-01 02:30:48 27 4
gpt4 key购买 nike

我正在尝试使用 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;
}

我遇到了这个错误

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

如何解决这个问题

最佳答案

注意:我是 EclipseLink JAXB (MOXy) 的负责人,也是 JAXB 2 (JSR-222) 专家组的成员。

如果您最终正在寻找一种使用 JAXB (JSR-222) 实现与 JSON 交互的方法,那么下面是如何使用 MOXy 完成的。 Jettison 是一个有趣的库,但使用它时会遇到一些问题:

演示

仅使用标准的 Java SE API。有两个 MOXy 特定属性需要在 Unmarshaller 上设置:“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<MiAppUsage> 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<MiAppUsage> appUsage;

}

MiApp 使用

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

了解更多信息

关于java - 使用 Jettison 进行 JSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9924567/

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