javaBean Convert to Json Str
来源:互联网 发布:程序员真的很累吗 编辑:程序博客网 时间:2024/05/20 02:27
package com.jumu.jygl.actions.student;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import com.jumu.util.StringUtil;
/**
* 支持JavaBean的JSONObject
* @author yang.liu
*/
public class JSON {
public JSON() {
}
private static Map types;
// public static void main(String[] args) {
// JyStuInfo stu = new JyStuInfo("测试成功!");
// stu.setOrgName("adf");
// stu.setStuInfoAddress("dddddd");
// stu.setMajorName("major爱的'f");
// String str=JSON.getJsonByObject(stu);
// System.out.println("str:"+str);
// }
public static String getJsonByObject(Object obj) {
JSON.initTypes();
StringBuffer jsonStr = new StringBuffer(128);
try {
jsonStr.append("{");
Class cls = obj.getClass();
//获取类属性集合
Field f[] = cls.getDeclaredFields();
String fName, getMethodName, type = "";
for (int i = 0; i < f.length; i++) {
fName = f[i].getName();
type = f[i].getType().getCanonicalName();
type = type.substring(type.lastIndexOf(".") + 1);
if (types.containsValue(type.toUpperCase())) { //如果存在此类型 .. .
if (!type.equalsIgnoreCase("boolean")) {
getMethodName = "get" + fName.substring(0, 1).toUpperCase() + fName.substring(1);
} else { //boolean 类型的字段生成的方法比较特殊,在此做简单的处理
if ("is".equals(fName.substring(0, 2)) && (fName.substring(0, 1).equals(fName.substring(0, 1).toUpperCase()))) {
getMethodName = fName;
} else {
getMethodName = "is" + fName.substring(0, 1).toUpperCase() + fName.substring(1);
}
}
Method getMethod =null;
try{
//得到方法
getMethod= cls.getMethod(getMethodName);
}catch(Exception e){
//出错说明没有该字段的 getter 方法 跳过此字段继续下一个
continue;
}
//执行此方法得到结果
Object result = getMethod.invoke(obj);
if (result == null) {
jsonStr.append(fName).append(":''").append(",");
} else if (StringUtil.isEmpty(result.toString())) {
jsonStr.append(fName).append(":''").append(",");
} else {
result=StringUtil.string2Json(result.toString());
result=StringUtil.encode(result.toString());
jsonStr.append(fName).append(":'").append(result).append("',");
}
}
}
//截取最后一个 【,】
if(jsonStr.length()>1){
jsonStr.setLength(jsonStr.length()-1);
}
jsonStr.append("}");
} catch (Exception e) {
e.printStackTrace();
}
return jsonStr.toString();
}
public Map getTypes() {
return types;
}
public void setTypes(Map types) {
this.types = types;
}
public static void initTypes(){
//可转换的数据类型, 如果 pojo是与数据库相关联那么 set 属性的可能是一对多的管理 在此不做操作
if (types == null || types.size() == 0) {
types = new HashMap<String, String>();
types.put("String", "STRING");
types.put("Integer", "INTEGER");
types.put("int", "INT");
types.put("Long", "LONG");
types.put("boolean", "BOOLEAN");
types.put("byte", "BYTE");
types.put("short", "SHORT");
types.put("float", "FLOAT");
}
}
// private boolean abCed;
// private boolean isAbc;
// private boolean IsACC;
// private boolean issAb;
//
// public boolean isAbCed() {
// return abCed;
// }
//
// public void setAbCed(boolean abCed) {
// this.abCed = abCed;
// }
//
// public boolean isAbc() {
// return isAbc;
// }
//
// public void setAbc(boolean abc) {
// isAbc = abc;
// }
//
// public boolean isIsACC() {
// return IsACC;
// }
//
// public void setIsACC(boolean isACC) {
// IsACC = isACC;
// }
//
// public boolean isIssAb() {
// return issAb;
// }
//
// public void setIssAb(boolean issAb) {
// this.issAb = issAb;
// }
}
- javaBean Convert to Json Str
- str convert to int
- json convert to string
- Object convert to Json string
- Convert object to json and json to object in C#
- convert Java object to / from JSON
- Convert Xml to json object in android
- swift dictonary convert to (转为 )json
- json str
- java bean convert to json string and json string convert to java bean
- Java JSON - Convert Java Object To / From JSON (Jackson)
- How to convert XML to JSON in ASP.NET C#
- How to convert Java object to / from JSON (Gson)
- How to convert XML to JSON in ASP.NET C#
- How to convert Java object to / from JSON (Gson)
- python:字符串和数字拼接 (TypeError: Can't convert 'int' object to str implicitly)
- selenium--unittest字符串/整型问题Can't convert 'int' object to str implicitly提示解决方法
- IOS json转对象 /json 转 Object / ios json to Object / ios json convert Object
- 撒的发撒范德萨
- 外观模式和适配器模式的区别
- Java web热部署 (Javarebel实现真正高效的tomcat热部署)
- android RelativeLayout布局属性
- 一篇关于java——collection的好文章
- javaBean Convert to Json Str
- TJU2248 Channel Design (最小树形图)
- Struts2中Session的使用方法详解
- 续“计算器”——添加modulus运算
- 百度旗下手机应用大盘点
- 利用AJAX模拟HTTP长连接(LongPoll)来实现”服务器推”技术 一级精华
- 忘记MySql的root密码如何修改
- Oracle ORA-00904: 无效的标识符
- jquery sortable 中文翻译 备用