当前位置: 首页 > 工具软件 > StringBean > 使用案例 >

String与实体类bean之间的转换

许琛
2023-12-01

实体类的转换与字符串的转换
需要转成的格式:
{locationid:‘1001’,locationname:‘张三’,locationx:‘nn’}

{name:’name’,name1:’name1’}

通用的方法:

public static User stringtobean_User(String str) {
		String c=str.substring(str.indexOf("["), str.lastIndexOf("]")+1);
		c=c.replace("[", "{");
		c=c.replace("]", "'}");
		c=c.replace("=", ":'");
		c=c.replace(",", "',");
		System.out.println("接收到去格式的实体类"+c.toString());
		JSONObject jsonObject=JSONObject.fromObject(c);
		User strbean=(User)JSONObject.toBean(jsonObject, User.class);
		return strbean;
		}

使用maven的时候导入的pom

	<dependency>
		<groupId>net.sf.json-lib</groupId>
		<artifactId>json-lib</artifactId>
		<version>2.4</version>
		<classifier>jdk15</classifier>
	</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.7.0</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.5</version>
		</dependency>
		<dependency>
			<groupId>net.sf.ezmorph</groupId>
			<artifactId>ezmorph</artifactId>
			<version>1.0.3</version>
	
		</dependency>
 类似资料: