编程语言通用JsonResult,返回格式化json字符串,使用简单

编程语言通用JsonResult,返回格式化json字符串,使用简单

用法:直接把data放到里面

List data = teamService.selEvent();

JsonResult j1 = new JsonResult(data); //直接把data放到里面了

引入依赖:pom.xml

    com.alibaba
    fastjson
    1.2.41

返回值示例:

{
"success":true,
"message":null,
"errorCode":"0",
"errorMsg":"操作成功",
"total":0,
"data":[
{
"eventcontent":"街边摊贩占道经营,影响行人交通,造成拥堵。",
"problem":"街边摊贩占道经营,影响行人交通,造成拥堵。"
}
]
}

详细代码:

package com.example.Entity;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用JsonResult
 */
@Data
public class JsonResult {
    private static final long serialVersionUID =-123847128341023033L;
    @JSONField
    private boolean success = true;
    @JSONField
    private String message = null;
    @JSONField
    private String errorCode = "0";
    @JSONField
    private String errorMsg = "";
    @JSONField
    private Integer total = 0;
    @JSONField
    private List data = new ArrayList();

    public JsonResult() {
    }

    /**
     * 当有异常时,直接throw一个实现ErrorCode的异常类
     * 通过global异常处理器,就可以把jsonResult封装起来,这样代码简洁优美
     * 如果没有BaseException可以注释掉这个方法
     */
//    public JsonResult(BaseException exception) {
//        if (exception != null) {
//            success = false;
//            errorCode = exception.getErrorCode();
//            errorMsg = exception.getErrorMsg();
//        }
//    }
    /**
     *  虽然很多人都写为isSuccess(),但强烈不建议,因为相当于getSuccess()
     *  可以用idea的自动生成下,如果有isSuccess(),就不会生成getSuccess()
     */
    public boolean successFlag() {
        return success;
    }

    public JsonResult(List data) {
        if (data != null && data.size() > 0) {
            this.data = data;
            this.message = "操作成功";
        }else {
            this.message = "操作失败";
            this.errorCode = "405";
            this.errorMsg = "返回值为null";
        }
    }

    public JsonResult(T data) {
        if (data != null) {
            this.data.add(data);
            this.message = "操作成功";
        }else {
            this.message = "操作失败";
            this.errorCode = "405";
            this.errorMsg = "返回值为null";
        }
    }
    //  失败情况的构造  只用errorCode,errorMsg即可
    public JsonResult(String errorCode,String errorMsg) {
        this.success=false;
        this.errorCode=errorCode;
        this.errorMsg=errorMsg;
    }
}

文章来源:https://www.cnblogs.com/buguaiya/archive/2021/11/10/15534299.html

 

阿哇教育
www.awaedu.com
搜白度
www.sobd.cc
教程弟
www.jcdi.cn
搜码吧
www.somanba.cn
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Excel格式的字符串转换为JSON,您可以使用Java中的Apache POI库来读取Excel文件并从中提取数据,然后将数据转换为JSON格式。下面是一个简单的示例代码片段,它演示了如何将Excel文件中的数据转换为JSON格式: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import com.fasterxml.jackson.databind.ObjectMapper; public class ExcelToJsonConverter { public static void main(String[] args) { try { // Load Excel file FileInputStream file = new FileInputStream(new File("input.xlsx")); Workbook workbook = WorkbookFactory.create(file); // Read Excel sheet and convert to JSON Sheet sheet = workbook.getSheetAt(0); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); for (Row row : sheet) { Map<String, String> map = new HashMap<String, String>(); for (Cell cell : row) { map.put(cell.getStringCellValue(), cell.getStringCellValue()); } list.add(map); } ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(list); System.out.println(json); // Close file file.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先使用Apache POI库加载Excel文件,然后使用WorkbookFactory创建工作簿。接下来,我们选择第一个工作表并将其转换为JSON格式。最后,我们使用Jackson库的ObjectMapper将List<Map<String, String>>对象转换为JSON字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值