用阿里的fastJson.jar进行list转成json字符串
程序员文章站
2022-06-17 10:24:50
...
package com.zcwSpringBoot.zcw.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zcwSpringBoot.zcw.entiy.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* @author zhaocunwei
* @ClassName: ListTOJSONController
* @Description: TODO
* @date 2019/11/13 11:24
*/
@RestController
public class ListTOJSONController {
@RequestMapping("/listToJson")
public String getListToJson(){
User user = new User();
user.setAge(10);
user.setName("测试");
user.setId(1);
List<User> userList = new ArrayList<>();
userList.add(user);
return JSON.toJSON(userList).toString();
}
}
测试结果: