您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在JavaWeb項目中將實體類轉換為json對象,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
實體類User代碼:
package com.neuedu.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; /** * Keafmd * * @ClassName: User * @Description: user實體類 * @author: 牛哄哄的柯南 * @date: 2020-12-17 14:08 */ @Data @AllArgsConstructor @NoArgsConstructor public class User { private Integer id; private String name; private String address; private Date date; }
我沒有自己寫構造函數和Setter(),Getter()以及toString()方法,而是使用了lombok自動生成的。
添加依賴:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency>
JsonTest 代碼:
package com.neuedu.entity; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import org.junit.Test; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Keafmd * * @ClassName: Test * @Description: user測試類 * @author: 牛哄哄的柯南 * @date: 2020-12-17 14:10 */ public class JsonTest { @Test public void test(){ List userList = new ArrayList(); userList.add(new User(1,"牛哄哄的柯南1","某某省某某市1",new Date())); userList.add(new User(2,"牛哄哄的柯南2","某某省某某市2",new Date())); userList.add(new User(3,"牛哄哄的柯南3","某某省某某市3",new Date())); userList.add(new User(4,"牛哄哄的柯南4","某某省某某市4",new Date())); userList.add(new User(5,"牛哄哄的柯南5","某某省某某市5",new Date())); userList.add(new User(6,"牛哄哄的柯南6","某某省某某市6",new Date())); //List集合轉換成 json String json = JSON.toJSONString(userList, SerializerFeature.WriteDateUseDateFormat ); System.out.println(json); } }
運行結果:
[{"address":"某某省某某市1","date":"2020-12-17 21:09:21","id":1,"name":"牛哄哄的柯南1"},
{"address":"某某省某某市2","date":"2020-12-17 21:09:21","id":2,"name":"牛哄哄的柯南2"},
{"address":"某某省某某市3","date":"2020-12-17 21:09:21","id":3,"name":"牛哄哄的柯南3"},
{"address":"某某省某某市4","date":"2020-12-17 21:09:21","id":4,"name":"牛哄哄的柯南4"},
{"address":"某某省某某市5","date":"2020-12-17 21:09:21","id":5,"name":"牛哄哄的柯南5"},
{"address":"某某省某某市6","date":"2020-12-17 21:09:21","id":6,"name":"牛哄哄的柯南6"}]Process finished with exit code 0
UserController代碼:
package com.neuedu.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.neuedu.entity.User; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Keafmd * * @ClassName: UserController * @Description: UserController * @author: 牛哄哄的柯南 * @date: 2020-12-17 14:20 * * * http://127.0.0.1:8080/web/user */ @WebServlet(name="UserController" ,urlPatterns = "/user") public class UserController extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List userList = new ArrayList(); userList.add(new User(1,"牛哄哄的柯南1","某某省某某市1",new Date())); userList.add(new User(2,"牛哄哄的柯南2","某某省某某市2",new Date())); userList.add(new User(3,"牛哄哄的柯南3","某某省某某市3",new Date())); userList.add(new User(4,"牛哄哄的柯南4","某某省某某市4",new Date())); userList.add(new User(5,"牛哄哄的柯南5","某某省某某市5",new Date())); userList.add(new User(6,"牛哄哄的柯南6","某某省某某市6",new Date())); String json = JSON.toJSONString(userList, SerializerFeature.WriteDateUseDateFormat ); resp.setContentType("application/json"); resp.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); out.write(json); out.flush(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
啟動服務訪問http://127.0.0.1:8080/web/user
訪問結果:
上述就是小編為大家分享的怎么在JavaWeb項目中將實體類轉換為json對象了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。