网上前辈们的写法,然后自己精简出来,自己记录,亲自测试,可行
在pom.xml中添加如下配置
<
dependency
>
<
groupId
>
net.sf.json-lib
</
groupId
>
<
artifactId
>
json-lib
</
artifactId
>
<
version
>
2.4
</
version
>
<
classifier
>
jdk15
</
classifier
>
</
dependency
>
对象转字符串:
JSONObject jsonObject = JSONObject.
fromObject
(dto);
jsonObject.toString()
json字符串转对象:
json字符串:msg
类对象
WebMsgDto
JSONObject
jsonObject =
JSONObject
.
fromObject
(msg);
if
(jsonObject ==
null
) {
return
;
}
WebMsgDto dto = (WebMsgDto)
JSONObject
.
toBean
(jsonObject, WebMsgDto.
class
);