手写 json

来源:互联网 发布:javbus最新域名 编辑:程序博客网 时间:2024/04/29 22:39

发现新公司公司竟然手写json 不用flexJSON和jsonwrapper .好吧。。

上代码

public String bean2JsonInCombox(List resultList, String id, String text,
            String idType) throws Exception {
        Object idValue = null;
        StringBuffer str = new StringBuffer("{\"totalCount\": 1,\"data\":[");
        if (null != resultList && 0 < resultList.size()) {
            for (Iterator iter = resultList.iterator(); iter.hasNext();) {
                Object obj = iter.next();
                if (idType.equalsIgnoreCase("Integer")) {
                    idValue = (Integer) obj
                            .getClass()
                            .getMethod(
                                    "get" + id.substring(0, 1).toUpperCase()
                                            + id.substring(1))
                            .invoke(obj, null);
                }
                if (idType.equalsIgnoreCase("String")) {
                    idValue = (String) obj
                            .getClass()
                            .getMethod(
                                    "get" + id.substring(0, 1).toUpperCase()
                                            + id.substring(1))
                            .invoke(obj, null);
                }
                String textValue = (String) obj
                        .getClass()
                        .getMethod(
                                "get" + text.substring(0, 1).toUpperCase()
                                        + text.substring(1)).invoke(obj, null);
                str.append("{ \"id\" : ");
                str.append("\"" + idValue + "\",");
                str.append(" \"text\" : ");
                str.append(" \"" + textValue + "\"");
                str.append("},");
            }
            str.delete(str.length() - 1, str.length());
        }
        str.append("]}");
        return str.toString();
    }

0 0