博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【反射】解析json 为例
阅读量:7224 次
发布时间:2019-06-29

本文共 4116 字,大约阅读时间需要 13 分钟。

hot3.png

好久没有弄过反射了,今天周末,想起来复习了下!

Person实体类

 

package com.masque.json;import java.util.Date;/*** * @title: 实体信息* @description: json对应封装的实体* @className: Person.java* @author: masque* @createDate: 2013-7-27 * @version: 1.0*/public class Person {	private String name;	private Date birthday;	private Short sex;	private Boolean isMarry;	private Double high;	private Float weight;	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public Date getBirthday() {		return birthday;	}	public void setBirthday(Date birthday) {		this.birthday = birthday;	}	public Short getSex() {		return sex;	}	public void setSex(Short sex) {		this.sex = sex;	}	public Boolean getIsMarry() {		return isMarry;	}	public void setIsMarry(Boolean isMarry) {		this.isMarry = isMarry;	}	public Double getHigh() {		return high;	}	public void setHigh(Double high) {		this.high = high;	}	public Float getWeight() {		return weight;	}	public void setWeight(Float weight) {		this.weight = weight;	}	}

解析过程

 

 

package com.masque.json;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.*;/** *  * @title: 反射 * @description: 用来解析json并封装数据 * @className: ObjToObj.java * @createDate: 2013-7-27 * @version: 1.0 */public class ObjToObj {		@SuppressWarnings("all")	public static void main(String[] args) {		//实体对应的字符串形式		String personStr = "{'name':'xiao','birthday':'1989-02-22','sex':'1','isMarry':'false','high':'165.5','weight':'60.55'}"; 		int flag = 1;		//想将字符串解析成Map		Map
inMap = new HashMap
(); while(flag!=-1){ flag = personStr.indexOf("','"); if(flag==-1) continue; String pro = personStr.substring(1,flag+1); personStr = personStr.substring(flag+2); String [] pp = pro.replaceAll("'", "").split(":"); inMap.put(pp[0], pp[1]); } Iterator
it = inMap.keySet().iterator(); Class obj=null; Object a = null; try { obj = Class.forName("com.masque.json.Person");//反射得到类 a = obj.newInstance();//实例化对象 while (it.hasNext()) { String key = it.next(); Method[] methods = obj.getMethods();//得到所有方法的对象数组 String m = key.substring(0,1).toUpperCase()+key.substring(1); for (int i = 0; i < methods.length; i++) { if(methods[i].getName().equals("get"+m)){//get方法才有返回值! //得到返回值类型 String returnType = methods[i].getReturnType().getSimpleName(); System.out.println(returnType); Method method = null; //通过判断属性的类型来转换到对应的对象 if(returnType.equals("Date")){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); method = obj.getDeclaredMethod("set"+m, Date.class); method.invoke(a,format.parse(inMap.get(key))); } if(returnType.equals("Short")){ method = obj.getDeclaredMethod("set"+m, Short.class); method.invoke(a,Short.parseShort(inMap.get(key))); } if(returnType.equals("Boolean")){ method = obj.getDeclaredMethod("set"+m, Boolean.class); method.invoke(a,Boolean.parseBoolean(inMap.get(key))); } if(returnType.equals("Double")){ method = obj.getDeclaredMethod("set"+m, Double.class); method.invoke(a,Double.parseDouble(inMap.get(key))); } if(returnType.equals("Float")){ method = obj.getDeclaredMethod("set"+m, Float.class); method.invoke(a,Float.parseFloat(inMap.get(key))); } if(returnType.equals("String")){ method = obj.getDeclaredMethod("set"+m, String.class); method.invoke(a,inMap.get(key)); } } } } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); }catch (ClassNotFoundException e) { e.printStackTrace(); }catch (InstantiationException e) { e.printStackTrace(); } System.out.println(((Person)a).getName()+"省略。。。"); }}

 

转载于:https://my.oschina.net/bughope/blog/403724

你可能感兴趣的文章
第四次实验
查看>>
IOS UIView 03- 自定义 Collection View 布局
查看>>
instantclient的使用入门
查看>>
【数据结构作业心得】4-1 指针笔记
查看>>
oracle拼接字段用||
查看>>
rabbitmq /usr/lib/rabbitmq/bin/rabbitmq-server: line 85: erl: command not found
查看>>
编程语言学习清单
查看>>
IO流的复习笔记
查看>>
MySQL Connector Net连接vs2012问题
查看>>
LeetCode – Refresh – Merge Intervals
查看>>
UDP编程简单案例
查看>>
Exce信息提取
查看>>
基于c的简易计算器一
查看>>
POJ1995 ZOJ2150 Raising Modulo Numbers【快速模幂】
查看>>
NTT学习笔记
查看>>
online_judge_1051
查看>>
Mac与Widow下编译与运行java文件引入多个外部jar包
查看>>
通过Bottledwater同步PostgreSQL中的数据变化到Kafka消息队列
查看>>
CSS Hack兼容
查看>>
ASP.NET WebService 中使用 ASP.NET_SessionId
查看>>