package myp;
import java.lang.reflect.*;
import java.util.*;
/**
* @author 惠万鹏
* @time 2008-06-13
* @version 1.0
*/
public class JSON {
/**
* <p>
* 本方法用利用反射机制根据对像的域名获得PO的getter方法名
* </p>
*
* @param o
* :po对像
* @return getter方法名
*/
private synchronized static String[][] gFMs(Object o) {
String[][] fms = null;
if (o != null)
{
Field flist[] = o.getClass().getDeclaredFields();
fms = new String[flist.length][2];
for (int i = 0; i < flist.length; i++)
{
fms[i] = JSON.assemblyFM(flist[i].toString());
}
}
return fms;
}
/**
* <p>
* 根据域名组装域名和方法名
* </p>
*
* @param sfield
* @return
*/
private synchronized static String[] assemblyFM(String sfield)
{
String[] fm = new String[2];
int len = sfield.length();
int pointAt = sfield.lastIndexOf(".") + 1;
fm[0] = sfield.substring(pointAt, len);
fm[1] = String.valueOf(fm[0].charAt(0)).toUpperCase();
if (fm[0].length() > 1)
{
fm[1] += fm[0].substring(1, fm[0].length());
}
if (sfield.indexOf("boolean") > 0)
{
fm[1] = "is" + fm[1];
}
else
{
fm[1] = "get" + fm[1];
}
fm[1] += "()";
return fm;
}
/**
* <p>po的方法里是否含有域的getter方法</p>
* @param fms
* @param method
* @return
*/
private static String getFN(String[][] fms, String method)
{
if(fms != null)
{
int len = method.length();
int pointAt = method.lastIndexOf(".") + 1;
String methodName = method.substring(pointAt, len);
for(int i = 0; i < fms.length; i++)
{
if(methodName.equals(fms[i][1]))
{
return fms[i][0];
}
}
}
return "";
}
/**
* <p>得到单个po的json info</p>
* @param o
* @return
*/
private static String getJsonInfo(Object o) {
StringBuffer jsonInfo = new StringBuffer("{");
String[][] fms = JSON.gFMs(o);
if(fms != null){
String fn = "";
String fv = "";
Method[] methods = o.getClass().getDeclaredMethods();
for(int i = 0; i < methods.length; i++)
{
fn = JSON.getFN(fms,methods[i].toString());
if(fn != null && !fn.equals(""))
{
jsonInfo.append("\"");
jsonInfo.append(fn);
jsonInfo.append("\"");
jsonInfo.append(":");
jsonInfo.append("\"");
try
{
fv = String.valueOf(methods[i].invoke(o, (Object[])null));
fv = fv.equals("null") ? "" : fv;
jsonInfo.append(fv);
}
catch(Exception e)
{
jsonInfo.append("");
}
jsonInfo.append("\",");
}
}
}
int len = jsonInfo.length();
String strEnd = jsonInfo.substring(len - 1, len);
if(strEnd != null && strEnd.equals(","))
{
jsonInfo.delete(len - 1, len);
}
jsonInfo.append("}");
return jsonInfo.toString();
}
/**
* <p>得到一个集合的json信息</p>
* @param list
* @return
*/
public static String getJsonInfos(List<Object> list){
StringBuffer infos = new StringBuffer();
infos.append("{[");
if(list != null)
{
Iterator<Object> itObj = list.iterator();
while(itObj.hasNext())
{
infos.append(JSON.getJsonInfo(itObj.next()));
if(itObj.hasNext())
{
infos.append(",");
}
}
}
infos.append("]}");
return infos.toString();
}
public static void main(String[] args) {
List<Object> list = new ArrayList<Object>();
PersonPO po = new PersonPO();
po.setAge("25");
//po.setName("惠万鹏");
po.setSex("男");
po.setParty(true);
list.add(po);
po = new PersonPO();
po.setAge("27");
po.setName("惠帆");
po.setSex("女");
po.setParty(false);
list.add(po);
System.out.println(JSON.getJsonInfos(list));
}
}