- 从String中转成JSON对象
最近使用json来传递对象,JAXB,基于JAVA annotation,因此代码简洁,所以选用支持JAXB的MOXy,以前一直用google Gson,这个适合手动解析JSON,它在一些很大的JSON对象上速度很快,但是不利于快速开发,且维护复杂,代码不友好。
Gson中的fromJson 方法也可以解析简单对象,如{"type":0,"score":0.0}。但复杂对象,包含数组,内嵌其他对象,我没有成功。
第一次使用MOXy,版本2.5.0,解析JSON出错:
MOXy deserialization exception: A descriptor with default root element was not found in the project
网上查了下,statckoverflow的
上面说是没有指定root属性:
public static <T>T fromJson(StreamSource streamSource, Class<?> cls, boolean hasRoot)
throws JAXBException, ClassCastException {
Map<String, Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, hasRoot);//---->这个需要设为false
.createContext(new Class[] { cls }, properties);
Unmarshaller un = jc.createUnmarshaller();
JAXBElement<T> elem = (JAXBElement<T>) un.unmarshal(streamSource,cls);//-->当JSON不含root时,必须指定cls类型。所以,这里必须要用StreamSource,而不能直接用reader,否则没法指定cls类型。
return (T)(elem.getValue());
}
import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; import org.eclipse.persistence.jaxb.JAXBContextProperties; public class DtoUtil { @SuppressWarnings("unchecked") public static <T>T fromJson(StreamSource streamSource, Class<?> cls, boolean hasRoot) throws JAXBException, ClassCastException { Map<String, Object> properties = new HashMap<String, Object>(2); properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, hasRoot); JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory .createContext(new Class[] { cls }, properties); Unmarshaller un = jc.createUnmarshaller(); JAXBElement<T> elem = (JAXBElement<T>) un.unmarshal(streamSource,cls); return (T)(elem.getValue()); } public static <T> String toJson(T obj,Class<?>cls,boolean hasRoot) throws JAXBException, ClassCastException { Map<String, Object> properties = new HashMap<String, Object>(2); properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, hasRoot); JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory .createContext(new Class[] { cls }, properties); Marshaller marshaller = jc.createMarshaller(); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); return writer.toString(); } }
maven dependency:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
</dependency>
用法,如:
DtoUtil.fromJson(new StreamSource(reader), cls, hasRoot);
DtoUtil.toJson(obj, cls, hasRoot);
相关推荐
JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件
eclipse-jaxb 2021年3月20日,星期六 已经有很多关于如何使JAXB与Java11和OSGI平台(如Eclipse)一起使用的文章。对于Java8,JAXB(Java XML绑定)API和实现是Java平台和“标准”的一部分。使用Java9时,不建议将其...
下载eclipselink.jar,这个jar包版本2.5.2,下载eclipselink.jar
eclipselink 1.0 doc 文档
详细讲述了EclipseLink的使用方法。EclipseLink的前身是Oracle的TopLink.
有关myesql与eclipse连接jdbc中有关工具类编写的范例
eclipselink的英文文档
找到的一个eclipselink增删改查
添加jobss下eclipselink的module实现实体自动扫描
JPA推荐的实现: EclipseLink
jsonEditor Eclipse 插件
EclipseLink 测试出错程序, 此程序是正对Glassfish V3.1的EclipseLink的,目前发现V3.1中的PessimisticLock出现问题
主要是描述一些插件在eclipse中link时,出现的一些问题说明的解决方案
1、根据Gson库使用的要求,将JSON结构格式解析成实体类 2、一键解决复杂JSON ,快速实现JavaBean。 一句话就可以将复杂的JSON数据解析成相应的Bean对象。 JavaBean javaBean=gson.fromJson(jo.toString(),Json...
jar包,官方版本,自测可用
NULL 博文链接:https://remotejavasky.iteye.com/blog/1182721
jar包,官方版本,自测可用
jar包,官方版本,自测可用
工作台主要用于在使用本机EclipseLink API,MOXy或EIS时进行开发。 使用JPA时,可以使用其他开发工具,例如Eclipse Dali或Oracle JDeveloper。要求JDK 1.8 JAVA_HOME环境变量设置为JDK 1.8文献资料讨论与新闻建造...