论坛首页 Java企业应用论坛

将实体转成JSON实体

浏览 9530 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (9) :: 隐藏帖 (1)
作者 正文
   发表时间:2009-10-07   最后修改:2009-10-10
http://www.iteye.com/topic/484519已发布了成型工具包,还包括相关工具.



定义一个注解


@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.FIELD })

public @interface JSONValue {

}



实体

package com.ask.admin.entity;

public class Favorite implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8350231152253261408L;

	// 编号
	@JSONValue
	private int id;
	// 标题
	@JSONValue
	private String title;
	// 地址
	@JSONValue
	private String uri;
	// 分类
	private int fCategory;
	// 描述
	@JSONValue
	private String fdesc;
	// 状态
	private int status;

	private int author;

	private FavoriteCategory favoriteCategory = new FavoriteCategory();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public int getFCategory() {
		return fCategory;
	}

	public void setFCategory(int category) {
		fCategory = category;
	}

	public String getFdesc() {
		return fdesc;
	}

	public void setFdesc(String fdesc) {
		this.fdesc = fdesc;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getAuthor() {
		return author;
	}

	public void setAuthor(int author) {
		this.author = author;
	}

	public FavoriteCategory getFavoriteCategory() {
		return favoriteCategory;
	}

	public void setFavoriteCategory(FavoriteCategory favoriteCategory) {
		this.favoriteCategory = favoriteCategory;
	}

}


转换函数
public class EntityConversionJSON {

	@AskLogger
	private static Logger logger;

	/**
	 * 实体的值转成JSON对象的值
	 * 
	 * @param source
	 * @param dest
	 */
	public static void entityToJSON(Object source, JSONObject dest) {

		Class clzss = source.getClass();

		Field[] fields = clzss.getDeclaredFields();

		try {
			for (Field field : fields) {
				//确认是否带有JSONValue注解
				if (field.getAnnotation(JSONValue.class) != null) {
					dest.put(field.getName(), getFieldValue(source, field
							.getName()));
				}
			}
		} catch (Exception e) {
			logger.error(e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获取字段的值
	 * 
	 * @param data
	 * @param fieldName
	 * @return
	 */
	public static Object getFieldValue(Object data, String fieldName) {
        
		StringBuilder sb = new StringBuilder();
		
		Class clzss = data.getClass();
		
		//将字段首字母大写
		String firstWord = fieldName.substring(0, 1).toUpperCase();
		sb.append(firstWord);
		sb.append(fieldName.substring(1, fieldName.length()));

		final String methodName = "get" + sb.toString();

		Method[] methods = clzss.getDeclaredMethods();
		try {
			for (Method method : methods) {
				// 调用对应的方法
				if (methodName.equals(method.getName())) {
					return method.invoke(data, new Object[] {});
				}
			}

		} catch (Exception e) {
			logger.error(e);
			throw new RuntimeException(e);
		}

		return null;

	}

}



运用

JSONObject jsonFavorite = new JSONObject();
			EntityConversionJSON.entityToJSON(favorite, jsonFavorite);

   发表时间:2009-10-09  
谢谢分享,一直在用JASONPLUGIN,有空自己实现一个,呵呵
0 请登录后投票
   发表时间:2009-10-09   最后修改:2009-10-09
思路不错,but json序列化还是用非入侵pojo的方式比较好。
而且太简单,没有处理常见数据类型和自定义类型。
0 请登录后投票
   发表时间:2009-10-09  
谢谢!领教了!
0 请登录后投票
   发表时间:2009-10-09  
同意kimmking。 到http://www.json.org/ 去下载 java相关的Source.  这样更好维护,也不容易出错。
0 请登录后投票
   发表时间:2009-10-09  
Ihavegotyou 写道
同意kimmking。 到http://www.json.org/ 去下载 java相关的Source.  这样更好维护,也不容易出错。

学习注解和反射很好的引导,当时只是为了方便没有想那么多.
谢谢指点!
0 请登录后投票
   发表时间:2009-10-09  
是个好办法,,之前我考虑的是用个类来解析,用 注解先标识一下也不错。
0 请登录后投票
   发表时间:2009-10-09  
写个通用的
能够json化单一对象
和json化树形集合的

0 请登录后投票
   发表时间:2009-10-09  
Ihavegotyou 写道
同意kimmking。 到http://www.json.org/ 去下载 java相关的Source.  这样更好维护,也不容易出错。


支持这两位说的我一直用官方那个 那个足够用了
0 请登录后投票
   发表时间:2009-10-09  
我用的nutz的解析功能,很简洁,支持LZ自己写
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics