list 与List打回前台
返回前台是对象用数组的形式拿出
===========================list<object>============================
===只要后台传的是对象不管是单个还是list,返回都是数组用0取第一个,可以去外层引号
@Modifying
@Query(value="select * from T_B_SJGD t  where  1=1 and id in (?1) order by id",nativeQuery=true)
public List<WorkOrder> queryWorkOrderProcessById(String id);




  public String queryWorkOrderProcessById() {
  String id=model.getId();
  WorkOrder a =new WorkOrder();
      List<WorkOrder> tr = new ArrayList<WorkOrder>();
      tr =this.getService().queryWorkOrderProcessById(id);
      if(tr.size()>0){
a=tr.get(0) ;
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"processLogs"});

String json = JSONArray.fromObject(a, config).toString();
  HttpServletResponse response =  ServletActionContext.getResponse();;
response.setCharacterEncoding("utf-8");
  //ServletOutputStream out = null;
  BufferedWriter write=null;
try
        {
write = new BufferedWriter(new OutputStreamWriter(response
.getOutputStream(), "UTF-8"));
write.write(json);
        // out = response.getOutputStream();
          //  out.write(json.getBytes());
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
        try {
        //out.flush();
//out.close();
        write.flush();
        write.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        }
        return null;
       
      } else{
        return null;
      }
 
 
  }


==========由于list中的原始数据是object所以返回前台的就是【object,object】===========================

var ry1;
             var rey2;
             var sb;
             var lx;
             var fl;
             var jb;
             var zy;
             var cs;
             var hs;
             var pjhs;
$.ajax({
        type:'post',
        url:urltro,
        async:false,
        data:null,
        dataType:'json',
        success:function(data){
===================这里是不需要eval的============
                var datas=eval(data);
                //$.each(datas,function(i){
               

                 //   });

              
                if(datas){
for(var j=0;j<datas.length;j++){
//alert(data[j].status);
           // alert(data[j].createTime);
ry1=data[j].bxlxr;
//alert(data[j].bxlxr);
rey2=data[j].XCCLR;
sb=data[j].relateResource;
lx=data[j].gzyelx;
fl=data[j].kind;
jb=data[j].gzdk;
zy=data[j].importance;
cs=data[j].outOfSla;
var name1 = document.getElementById("name1");
            var name2 =  document.getElementById("name2");
            var equipment =  document.getElementById("equipment");
            var lxt =  document.getElementById("lxt");
            var flt =  document.getElementById("flt");
            var jbt =  document.getElementById("jbt");
            var yxdt =  document.getElementById("yxdt");
            var slat =  document.getElementById("slat");
            var hst =  document.getElementById("hst");
            var evaltime =  document.getElementById("evaltime");
           if(ry1==""||ry1==null){
           ry1="未知";
            }
           if(rey2==""||rey2==null){
           rey2="未知";
            }
           if(sb==""||sb==null){
           sb="未知";
            }
           if(lx==""||lx==null){
           lx="未知";
            }
           if(fl==""||fl==null){
           fl="未知";
            }
           if(jb==""||jb==null){
           jb="未知";
            }
           if(zy==""||zy==null){
           zy="未知";
            }
           if(cs==""||cs==null){
           cs="未知";
            }
            var til=data[j].title;
            var stan=data[j].status;
            if(til==""||til==null){
            til="未知";
            }
           if(stan==""||stan==null){
           stan="未知";
            }
            name1.innerHTML +=ry1;
            name2.innerHTML +=rey2;
            equipment.innerHTML +=sb;
            lxt.innerHTML +=lx;
            flt.innerHTML +=fl;
            jbt.innerHTML +=jb;
            yxdt.innerHTML +=zy;
            slat.innerHTML +=cs;
            name.innerHTML +=til.substr(0,8);
            sta.innerHTML +=stan;
}
               
                    }
       
        },
        error:function(){
            //$.message("失败");
        }
}); 

==========================list<String[]>============================
@Modifying
@Query(value="select to_char(to_date(CREATETIME,'yyyy-mm-dd hh24:mi'),'yyyy') as a ,count(*) as b from T_B_SJGD group by  to_char(to_date(CREATETIME,'yyyy-mm-dd hh24:mi'),'yyyy') order by to_char(to_date(CREATETIME,'yyyy-mm-dd hh24:mi'),'yyyy')",nativeQuery=true)
public List<String[]> queryWorkOrderProcessCount();



public String queryWorkOrderProcessCount() {

List<String[]> count =  new ArrayList<String[]>();
count= this.getService().queryWorkOrderProcessCount();
//count.toArray(new String[count.size()])
//先转为一个大的数组
String[] toBeStored = convert(count);  
//JsonUtil.stringArrayToJsonStr(toBeStored)
String jsonstr= JsonUtil.stringArrayToJsonStr(toBeStored);
HttpServletResponse response=  ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
BufferedWriter write=null;
try {
write = new BufferedWriter(new OutputStreamWriter(response
.getOutputStream(), "UTF-8"));
write.write(jsonstr);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

        try {
        //out.flush();
//out.close();
        write.flush();
        write.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


return null;

}


static String[] convert(List<String[]> from) {
  ArrayList<String> list = new ArrayList<String>(); 
  for (Object[] strings : from) {
  String[] a = new String[strings.length];
//   for(int i=0;i<strings.length;i++){
//   a[i]=strings[i].toString();
//  
//   }
String b=  JSONArray.fromObject(strings).toString();
  Collections.addAll(list, b);
  } 
  return list.toArray(new String[list.size()]); 
  }

======================这种原始返回的list是string[]这种返回前台不是object需要用eval加工去【】""=========================
@SuppressWarnings("unchecked")
public String querySJ() {

List<String[]> count =  new ArrayList<String[]>();
count= this.getService().querySJ();
//count.toArray(new String[count.size()])

  String[] toBeStored = convert(count);  
  String jsonstr =JsonUtil.stringArrayToJsonStr(toBeStored);
// String jsonstr= JsonUtil.objectToJsonStr(count);
HttpServletResponse response=  ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
BufferedWriter write=null;
try {
write = new BufferedWriter(new OutputStreamWriter(response
.getOutputStream(), "UTF-8"));
write.write(jsonstr);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

        try {
        //out.flush();
//out.close();
        write.flush();
        write.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


return null;

}

function doDetail(){var table;
$.ajax({
url:'${ctx}/current-alarm!querySJ.json',
type:'POST',
data:'',
async:false,
success:function(data){
$("#detail-form").append('');
table = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
table +='<tr><th width="33%">创建时间</th><th width="33%">状态</th><th width="33%">当前处理人</th></tr>';
var detail =eval(data);
if(detail){
for(var i=0;i<detail.length;i++){
var tr='<tr>';
tr+='<td width="42%">'+detail[i][1]+'</td>';
tr+='<td width="10%">'+detail[i][2]+'</td>';
tr+='<td width="48%">'+detail[i][3]+'</td>';
tr+='</tr>';
table += tr;
}
}
table += '</table>';
$("#detail-form").append(table);
}
});
document.getElementById("detail-form").style.display= "block";
$("#detail-form").dialog({
autoOpen : false,
height : 'auto',
width : 490,
modal : true,
onClose:function(){
$("#detail-form").empty();
$("#detail-form").attr("style", "display: none;overflow-y:auto;height:100%;");
}
});
$("#detail-form").dialog("open");
}

你可能感兴趣的:(json)