|
说实话,我也是a.b的坚定用户者,我自己也为此花费了不少功夫。但是我发现我一直都没有问为什么用_?
而只是问为什么不用 . 之前还可笑的改了源码支持了 . 这种取值方式 直到昨天,写了一个拼装json的代码
以为以前jeecg因为json的原因而不使用的,可是花了一个晚上,自己改了toJson这个方法可以返回嵌套对象了,but,but,我差改了之后easyui完全挂了,a.b这种值竟然没有任何作用
才想起,可能是easyui的原因,是easyui不支持a.b这种取值方式,而不是jeecg不想支持,今天网上百度了下
easyui源码,虽然是1.0.2版本,但是这也是基础的。发现
尼玛obj[field]可以取值才怪呢,我差,easyui真是个人才,可怜我两个晚上
ps1:easyui源码 http://jquery-easyui.googlecode.com/svn/trunk/src/
ps2:自己昨天写的tojson - /**
- * 循环LIST对象拼接EASYUI格式的JSON数据
- * @param fields
- * @param total
- * @param list
- */<span class="comment" style="color: rgb(0, 130, 0); width: auto; border: 0px; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; line-height: 18px; background-color: rgb(250, 250, 250);">可以避免掉hibernate的延长加载</span><span style="color: rgb(0, 0, 0); font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; line-height: 18px; background-color: rgb(250, 250, 250);"> </span>
- public static String listtojson(String[] fields, int total, List list,String[] footers) throws Exception {
- StringBuilder sb = new StringBuilder();
- sb.append("{"total":" + total + ","rows":[");
- Map<String,Object> fieldMap = getFieldMap(fields);
- for (int j = 0; j < list.size(); j++) {
- sb.append("{"state":"closed",");
- toJson(fieldMap,list.get(j),sb);
- if (j != list.size() - 1) {
- sb.append("},");
- } else {
- sb.append("}");
- }
- }
- sb.append("]");
- Object value;
- String footer;
- if(footers!=null){
- sb.append(",\'footer\':[{\'name\':\'合计\',");
- for(int i = 0;i<footers.length;i++){
- footer = footers[i];
- String footerFiled = footer.split(":")[0];
- if(footer.split(":").length==2){
- value = footer.split(":")[1];
- }else{
- value = TagUtil.getTotalValue(footerFiled,list);
- }
- sb.append("\'"+footerFiled+"\':\'"+value+"\'");
- if(i!=footers.length-1){
- sb.append(",");
- }
- }
- sb.append("}]");
- }
- sb.append("}");
- System.out.println(sb.toString());
- return sb.toString();
- }
-
-
- private static void toJson (Map<String,Object> fieldMap,Object obj,StringBuilder sb){
- Iterator<String> it = fieldMap.keySet().iterator();
- Object field,value;
- String key;
- String [] arr;
- while(it.hasNext()){
- key = it.next();
- field = fieldMap.get(key);
- if(field instanceof Map){
- sb.append("""+key+"":{");
- toJson((Map<String,Object>)field,obj,sb);
- sb.append("}");
- }else if(field instanceof String[]){
- arr = (String[])field;
- value = TagUtil.fieldNametoValues(arr[0], obj);
- sb.append(""" + arr[1] + """ + ":"" + value + """);
- }else{
- value = TagUtil.fieldNametoValues((String)field, obj);
- sb.append(""" + field + """ + ":"" + value + """);
- }
- if(it.hasNext()){
- sb.append(",");
- }
- }
- }
- /**
- * 组装对象,把一个对象的元素放到一起
- * @param fields
- * @return
- */
- private static Map<String, Object> getFieldMap(String[] fields) {
- Map<String,Object> results = new HashMap<String, Object>();
- for(int i = 0;i<fields.length;i++){
- if(fields[i].indexOf(".")>0){
- judgeFields(results,fields[i].split("\\."),fields[i]);//下面的多属性字段
- }else{
- results.put(fields[i], fields[i]);//这个对象的直接字段
- }
- }
- return results;
- }
-
- private static void judgeFields(Map<String, Object> results, String[] param,String field) {
- Map<String,Object> temp;
- for(int i = 0;i<param.length;i++){
- if(results.containsKey(param[i])){
- results = (Map<String,Object>)results.get(param[i]);
- }else{
- if(i==param.length-1){
- results.put(param[i],new String[]{field,param[i]});
- }else{
- temp = new HashMap<String, Object>();
- results.put(param[i],temp);
- results = temp;
- }
- }
- }
- }
复制代码 |
|