java jettison_java – 使用Jettison进行JSON解析

java jettison_java – 使用Jettison进行JSON解析我试图使用 Jettison 解析 JSON 对象 这是我正在使用的代码 Strings appUsage appName ANDROID totalUsers 0 appName IOS totalUsers 4 JSONObjectob newJSONObjec s ArrayListl1

我试图使用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 forum;

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 forum;

import java.util.ArrayList;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)

public class Root {

ArrayList appUsage;

}

MiAppUsage

package forum;

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/204100.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午9:00
下一篇 2026年3月19日 下午9:00


相关推荐

  • 研究学习之java使用selenium教程[通俗易懂]

    研究学习之java使用selenium教程[通俗易懂]提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结前言提示:这里可以添加本文要记录的大概内容:例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。提示:以下是本篇文章正文内容,下面案例可供参考一、pandas是什么?示例:pandas是基于NumPy的一种工具,该工具是为了解决数据分析任务而创建的。二、使用步骤1.引入库代码

    2022年6月28日
    40
  • 三菱modbus通讯实例 PLC如何设置_三菱plc网络通讯指令范例

    三菱modbus通讯实例 PLC如何设置_三菱plc网络通讯指令范例点击箭头处“工业之家”,选择“关注公众号”!三菱PLC控制机械手实例气动机械手动作示意图,其功能是将工件从A处移送到B处。气动机械手的升降和左右移行分别使用了双线圈的电磁阀,在某方向的驱动线圈失电时能保持在原位,必须驱动反方向的线圈才能反向运动。上升、下降对应的电磁阀线圈分别是YV2、YV1,右行、左行对应的电磁阀线圈分别是YV3、YV4。机械手的夹钳使用单线圈电磁阀YV5,线圈通电时夹…

    2025年10月20日
    7
  • linux 挂载raid_linux挂载磁盘阵列

    linux 挂载raid_linux挂载磁盘阵列在许多项目中,都会把数据存放于磁盘阵列,以确保数据安全或者实现负载均衡。在初始安装数据库系统和数据恢复时,都需要先挂载磁盘阵列到系统中。本文记录一次在linux系统中挂载磁盘的操作步骤,以及注意事项。此处所用操作系统为Asianux,磁盘阵列设备名为emcpowera,使用的分区为emcpowera1。1.使用命令fdisk–l/dev/emcpowera查看磁盘阵列的分区情况:2.正…

    2022年6月19日
    49
  • Python 类继承,__bases__, __mro__, super

    Python 类继承,__bases__, __mro__, super

    2021年12月14日
    43
  • 详细介绍如何从零开始制作51单片机控制的智能小车(一)———让小车动起来[通俗易懂]

    详细介绍如何从零开始制作51单片机控制的智能小车(一)———让小车动起来[通俗易懂]  从本文开始,在之后的一段时间里,我会通过本系列文章,详细介绍如何从零开始用51单片机去实现智能小车的控制,本文作为本系列的第一篇文章,主要介绍如何让小车动起来。一、硬件的选择  1、底盘和电机   底盘的形状呢,大家可以按照自己的需要自主选取,至于电机关注一下工作电压,转速,电机类型就差不多,对于新手,可以尝试以下样式(4WD智能小车底盘,附带4个直流减速电机,电机接线需要自己焊接),也就是本文例子采用的底盘和电机,组装简单,使用方便,特别适合新手。  2、电机驱动模块   

    2022年6月12日
    49
  • 分子模拟软件amber_[gromacs使用教程] 基于amber力场模拟蛋白小分子复合物

    分子模拟软件amber_[gromacs使用教程] 基于amber力场模拟蛋白小分子复合物祥请参考官网教程,使用其中的mdp参数文件(均100ps),案例只考虑模拟顺利,暂不考虑合理性。平台:windows软件:gaussina16,ambertools,gromacs2019.6,notepad++,spdbv4.10蛋白文件:4w52.pdb(配体选用EPE)小分子amber力场及坐标文件构建参考本公众号的案例蛋白的修复使用Notepad++删除小分子,水,保存文…

    2022年5月9日
    60

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号