Json转java对象

最近在最一个项目,项目中涉及到 json字符串 转换成 java对象,其中遇到一个问题,在这里记录一下。
首先,我是按照下面博文的方式进行转的,地址如下:

JSON字符串和java对象的互转【json-lib】

这个是上面博文中需要的jar包:要想实现JSON和java对象之间的互转,需要借助第三方jar包,这里使用json-lib这个jar包,下载地址为:https://sourceforge.net/projects/json-lib/,json-lib需要commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar五个包的支持,可以自行从网上下载,这里不再贴出下载地址。

根据博文中

        JSONObject jsonObject=JSONObject.fromObject(objectStr);
        Student stu=(Student)JSONObject.toBean(jsonObject, Student.class);

进行转换应该是没有问题,而我遇到的问题是:
1、不能转换
2、简化json串后,数值不正确。

贴一下我的代码:
实体类1:

public class TerminalStatusInstance {
	private int DevID;	
	private int BID;	
	private int CID;	
	private int LinkStatus;	
	private int DevStatus;	
	private int UnitCount;	
	private Unitn Unit1;	//对象类型属性[实体类二]
	private Unitn Unit2;
	
	//省略set/get方法
	//源代码是没有构造方法的。
	
}

实体类2:

public class Unitn {
	private Integer Count;
	private Integer UnitId;
	private Object[] Sta;
	//省略set/get方法
	//源代码是没有构造方法的。
}

测试json串转java对象:

public static void main(String[] args) {
		String str = "{\"BID\":61700,\"CID\":0,\"DevID\":102,\"DevStatus\":0,\"LinkStatus\":0,\"Unit1\":{\"Count\":118,\"Sta\":[\"1\",\"1\",\"1\",\"1\",\"1\",\"0\",0,\"1\",\"1\",1,\"1\",\"0\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\"],\"UnitId\":9},\"UnitCount\":14}";
//		String str = "{\"BID\":61700,\"CID\":3,\"DevID\":22,\"DevStatus\":3,\"LinkStatus\":3,\"UnitCount\":14}";
		JSONObject jsonObject=JSONObject.fromObject(str);
		TerminalStatusInstance log=(TerminalStatusInstance)JSONObject.toBean(jsonObject, TerminalStatusInstance.class);
		System.out.println(log);
}

运行之后报错:

2019-01-09 15:12:37,519 WARN [net.sf.json.JSONObject] - Tried to assign property DevID:java.lang.Integer to bean of class com.cetcnav.suo.ADSB.worker.TerminalStatusInstance
2019-01-09 15:12:37,521 WARN [net.sf.json.JSONObject] - Tried to assign property DevStatus:java.lang.Integer to bean of class com.cetcnav.suo.ADSB.worker.TerminalStatusInstance
2019-01-09 15:12:37,522 WARN [net.sf.json.JSONObject] - Tried to assign property LinkStatus:java.lang.Integer to bean of class com.cetcnav.suo.ADSB.worker.TerminalStatusInstance
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property 'Unit1' on class 'class com.cetcnav.suo.ADSB.worker.TerminalStatusInstance'
	at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy._setProperty(PropertySetStrategy.java:69)
	at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:60)
	at net.sf.json.JSONObject.setProperty(JSONObject.java:1394)
	at net.sf.json.JSONObject.toBean(JSONObject.java:443)
	at net.sf.json.JSONObject.toBean(JSONObject.java:233)
	at com.cetcnav.suo.ADSB.worker.TerminalContrastDifferenceWorker.main(TerminalContrastDifferenceWorker.java:127)
Caused by: java.lang.NoSuchMethodException: Unknown property 'Unit1' on class 'class com.cetcnav.suo.ADSB.worker.TerminalStatusInstance'
	at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2123)
	at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:890)
	at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy._setProperty(PropertySetStrategy.java:67)
	... 5 more

当时大体猜到应该是因为属性类型为对象类型的缘故,所以就省略了Unit1的值,在测试:

2019-01-09 15:35:28,456 WARN [net.sf.json.JSONObject] - Tried to assign property DevID:java.lang.Integer to bean of class com.cetcnav.suo.ADSB.worker.TerminalStatusInstance
2019-01-09 15:35:28,458 WARN [net.sf.json.JSONObject] - Tried to assign property DevStatus:java.lang.Integer to bean of class com.cetcnav.suo.ADSB.worker.TerminalStatusInstance
2019-01-09 15:35:28,459 WARN [net.sf.json.JSONObject] - Tried to assign property LinkStatus:java.lang.Integer to bean of class com.cetcnav.suo.ADSB.worker.TerminalStatusInstance
2019-01-09 15:35:28,459 WARN [net.sf.json.JSONObject] - Tried to assign property UnitCount:java.lang.Integer to bean of class com.cetcnav.suo.ADSB.worker.TerminalStatusInstance
TerminalStatusInstance [DevID=0, BID=61700, CID=3, LinkStatus=0, DevStatus=0, UnitCount=0, Unit1=null, Unit2=null]

数据不是想要的,DevID=0,
然后网上冲浪~~~
1、无构造函数,
2、Java中Json转换Bean对象并忽略大小写
3、使用json-lib JsonConfig 设置策略。
……
等等一些方法,无奈腹中墨水严重匮乏,没有解决。

然后就用了另外一个方法:ObjectMapper
使用 ObjectMapper类 必须引用3个jar包(jackson-databind,jackson-core,jackson-annotations)

 缺少 jackson-core ,会报错 The type com.fasterxml.jackson.core.JsonParser cannot be resolved. It is indirectly referenced from required .class files

 缺少 jackson-annotations ,会报错  Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect

但是要是用一个注解:@JsonProperty(value="")

ObjectMapper mapper = new ObjectMapper();
		try {
			TerminalStatusInstance t=(TerminalStatusInstance)mapper.readValue(str,TerminalStatusInstance.class);
			System.out.println(t);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

之后测试解决,后期再来搞上面的问题吧。

你可能感兴趣的:(json)