Json转java对象

时间:2025-01-29 08:55:47

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

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

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

根据博文中

        JSONObject jsonObject=(objectStr);
        Student stu=(Student)(jsonObject, );

进行转换应该是没有问题,而我遇到的问题是:
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=(str);
		TerminalStatusInstance log=(TerminalStatusInstance)(jsonObject, );
		(log);
}

运行之后报错:

2019-01-09 15:12:37,519 WARN [] - Tried to assign property DevID: to bean of class 
2019-01-09 15:12:37,521 WARN [] - Tried to assign property DevStatus: to bean of class 
2019-01-09 15:12:37,522 WARN [] - Tried to assign property LinkStatus: to bean of class 
Exception in thread "main" : : Unknown property 'Unit1' on class 'class '
	at $DefaultPropertySetStrategy._setProperty(:69)
	at $(:60)
	at (:1394)
	at (:443)
	at (:233)
	at (:127)
Caused by: : Unknown property 'Unit1' on class 'class '
	at (:2123)
	at (:890)
	at $DefaultPropertySetStrategy._setProperty(:67)
	... 5 more

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

2019-01-09 15:35:28,456 WARN [] - Tried to assign property DevID: to bean of class 
2019-01-09 15:35:28,458 WARN [] - Tried to assign property DevStatus: to bean of class 
2019-01-09 15:35:28,459 WARN [] - Tried to assign property LinkStatus: to bean of class 
2019-01-09 15:35:28,459 WARN [] - Tried to assign property UnitCount: to bean of class 
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  cannot be resolved. It is indirectly referenced from required .class files

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

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

ObjectMapper mapper = new ObjectMapper();
		try {
			TerminalStatusInstance t=(TerminalStatusInstance)(str,);
			(t);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			();
		}

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