JavaWeb实体类转为json对象的实现方法

1.创建个实体类

实体类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自动生成的。

2.使用类库 fastjson

添加依赖:

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.73</version>
</dependency>

3.测试一下

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

4.Controller代码

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

访问结果:

OK,到此就完成实体类转为json对象的需求了。更多相关Java实体类转为json对象内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • java中实体类和JSON对象之间相互转化

    在需要用到JSON对象封装数据的时候,往往会写很多代码,也有很多复制粘贴,为了用POJO的思想我们可以装JSON转化为实体对象进行操作 package myUtil; import java.io.IOException; import myProject.Student; import myProject.StudentList; import org.codehaus.jackson.map.ObjectMapper; import org.json.JSONArray; import or

  • JavaWeb实体类转为json对象的实现方法

    1.创建个实体类 实体类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-

  • java中实体类转Json的2种方法

    首先申明所需jar包: ezmorph-1.0.6.jar jackson-all-1.7.6.jar jsoup-1.5.2.jar 一.创建一个实体类Emp. package com.hyx.entity; public class Emp { private Integer id; private String name; private Integer dptNo; private String gender; private String duty; public Integer ge

  • .net实体类与json相互转换

    .net实体类与json相互转换时,注意要点: 1.jsonhelp编写时候添加的引用.System.Runtime.Serialization.Json;  2.实体类需声明为public jsonhelp代码:  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.Seri

  • Java读取项目json文件并转为JSON对象的操作

    Java读取项目json文件并转为JSON对象 1.创建json文件(demo.json) { "button": [ { "type": "click", "name": "今日歌曲", "key": "V1001_TODAY_MUSIC" }, { "name": "菜单", "sub_button":

  • 在python中将字符串转为json对象并取值的方法

    如下所示: string =" { "status": "error", "messages": ["Could not find resource or operation 'BZK1.MapServer' on the system."], "code": 404 }" print '对象:' string print '取值:' json.loads(string)['code']

  • C#编程获取实体类属性名和值的方法示例

    本文实例讲述了C#编程获取实体类属性名和值的方法.分享给大家供大家参考,具体如下: 遍历获得一个实体类的所有属性名,以及该类的所有属性的值 //先定义一个类: public class User { public string name { get; set; } public string gender { get; set; } public string age { get; set; } } //实例化类,并给实列化对像的属性赋值: User u = new User(); u.name

  • C#实现实体类与字符串互相转换的方法

    本文实例讲述了C#实现实体类与字符串互相转换的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.Text; namespace PackDLL.Data.ConvertData { /// <summary> /// 实体类.字符串互相转换 /// </summary> public class PackReflectionEntity<T> { //

  • jQuery序列化form表单数据为JSON对象的实现方法

    jquery提供的serialize方法能够实现. $("#searchForm").serialize(); 但是,观察输出的信息,发现serialize()方法做的是将表单中的数据以htpp请求格式拼接成字符串. serialize确实是能够解决一般的提交数据.但是有时我们需要的是一个object对象,而不是字符串(比如jqgrid reload时设置查询条件参数,就需要object对象). 方法如下: (function(window, $) { $.fn.serializeJs

  • springboot+mybatis通过实体类自动生成数据库表的方法

    前言 本章介绍使用mybatis结合mysql数据库自动根据实体类生成相关的数据库表. 首先引入相关的pom包我这里使用的是springboot2.1.8.RELEASE的版本 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</ve

随机推荐