java里对象转String类型,String类型转对象

mac2025-12-06  9

在pom.xml文件里先添加依赖

<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>

String类型转对象:

dataInfo dif = new dataInfo(); //对象 JSONObject jsonObject= JSONObject.fromObject(str); //str是字符串 dif = (dataInfo)JSONObject.toBean(jsonObject,dataInfo.class);

对象转String类型:

JSONObject.fromObject(dif).toString(); //dif是对象
最新回复(0)