Java中如何返回Json数组

期望返回如下格式的数据

{
  "ResultId": "1", 
  "ResultDetail": "Data", 
  "Data": [
    {
      "ListSeqNo": 2344, 
      "HeadSeqNo": 2342, 
      "SourcePath": "D:ABC", 
      "Source": "Schemas.rar", 
      "DestPath": "D:TransportSea", 
      "SourceType": "txt", 
      "IsRun": 1, 
      "RunParams": "", 
      "IsDown": "0", 
      "FileContents": ""
    }, 
    {
      "ListSeqNo": 8, 
      "HeadSeqNo": 2, 
      "SourcePath": "ABC", 
      "Source": "Schemas.rar", 
      "DestPath": "D:TransportSea", 
      "SourceType": "dir", 
      "IsRun": 1, 
      "RunParams": "", 
      "IsDown": "0", 
      "FileContents": ""
    }
  ]
}

Java代码如下 - 这个方法很好用

JSONObject jsonobject = new JSONObject();
List<Map<String, Object>> statusFlowList = null;
String  data = null;
//查询数据
statusFlowList = MSGService.getStatusFlow(statusFlow);
//处理数据 - 转为json字符串
if(null != statusFlowList && statusFlowList.size()>0) {
    data = JSON.toJSONString(statusFlowList, SerializerFeature.WriteMapNullValue);
    //data = data.replace("null", """");
}    
//处理数据 - 封装返回数据
jsonobject.put("result", "1");
jsonobject.put("info", "");
jsonobject.put("data", null==data? "" : data);
return jsonobject.toString();

Java代码如下 - 这个方法好笨啊

//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;

List<AmAppManagerConfig> aamConfigList = clsDataService.queryCLS008Two(msgForm);
if(null != aamConfigList && aamConfigList.size()>0) {
    //存在数据,封装数据进行返回
    JSONObject jsonObject08 = new JSONObject();
    info = "Data";
    result = "1";            
    jsonObject08.put("ResultId", result);
    jsonObject08.put("ResultDetail", info);
    JSONArray jsonArray = new JSONArray();
    for(AmAppManagerConfig am :aamConfigList) {
        JSONObject jsonObjectData = new JSONObject();
        jsonObjectData.put("ListSeqNo",am.getListSeqNo() != null ? am.getListSeqNo():""); //顺序号
        jsonObjectData.put("HeadSeqNo",am.getHeadSeqNo() != null ? am.getHeadSeqNo():"");  //头序号
        jsonObjectData.put("SourcePath",am.getSourcePath() != null ? am.getSourcePath():""); //目录地址
        jsonObjectData.put("Source",am.getSource() != null ? am.getSource():"");  //文件或目录名
        jsonObjectData.put("DestPath",am.getDestPath() != null ? am.getDestPath():"");  //应用所在目录
        jsonObjectData.put("SourceType",am.getSourceType() != null ? am.getSourceType():"");  //目录类型
        jsonObjectData.put("IsRun",am.getIsRun() != null ? am.getIsRun():"");      //可运行
        jsonObjectData.put("RunParams",am.getRunParams() != null ? am.getRunParams():""); //运行参数
        jsonObjectData.put("IsDown",am.getIsDown() != null ? am.getIsDown():"");   //是否下载
        jsonObjectData.put("FileContents",am.getFileContents() != null ? new String(am.getFileContents().getBytes((long)1, (int)am.getFileContents().length())):""); //INI配置  
        jsonArray.add(jsonObjectData);
        jsonObject08.element("Data", jsonArray);
        jsonObject08.getJSONArray("Data");
    }
    String decodeDataStr = new String(Base64.encodeBase64(jsonObject08.toString().getBytes()));
    System.out.println("decodeDataStr---->" + decodeDataStr);
    return decodeDataStr;
}else {
    //未查询到数据
    JSONObject jsonObject08 = new JSONObject();
    info = "客户端ID未授权";
    result = "0";            
    jsonObject08.put("ResultId", result);
    jsonObject08.put("ResultDetail", info);
    String decodeDataStr = new String(Base64.encodeBase64(jsonObject08.toString().getBytes()));
    return decodeDataStr;
}
原文地址:https://www.cnblogs.com/jkfeng/p/12124292.html