返回顶部
首页 > 资讯 > 后端开发 > Python >Java对象转Json,关于@JSONField对象字段重命名和顺序问题
  • 945
分享到

Java对象转Json,关于@JSONField对象字段重命名和顺序问题

2024-04-02 19:04:59 945人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录Java对象转JSON,@jsONField对象字段重命名和顺序一、引入Maven依赖二、字段重命名三、字段排序@JSONField注解常用的使用场景一、JSON内容与实体类,@

Java对象转Json,@JSONField对象字段重命名和顺序

一、引入maven依赖

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

二、字段重命名

1.创建一个测试实体

import lombok.Data; 
import java.io.Serializable;
 

@Data
public class LkWeChatBusinessLicenseInfo implements Serializable {
    private static final long serialVersionUID = 1582941630439552458L;
    private String businessLicenseCopy;
    private String businessLicenseNumber;
    private String merchantName;
    private String legalPerson;
    private String companyAddress;
    private String businessTime;
    public LkWeChatBusinessLicenseInfo(){
        this.businessLicenseCopy = "1";
        this.businessLicenseNumber = "2";
        this.merchantName = "3";
        this.legalPerson = "4";
        this.companyAddress = "5";
        this.businessTime = "6";
    }
}

2.将实体转换为json字符串,看看未转换前的效果

System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));

{
    "businessLicenseCopy":"1",
    "businessLicenseNumber":"2",
    "businessTime":"6",
    "companyAddress":"5",
    "legalPerson":"4",
    "merchantName":"3"
}

3.我们要转换为带下划线的key,例如把businessLicenseCopy转换为business_license_copy

我们需要修改实体,加上注解@JSONField

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import java.io.Serializable;
 

@Data
public class LkWeChatBusinessLicenseInfo implements Serializable {
    private static final long serialVersionUID = 1582941630439552458L;
    @JSONField(name = "business_license_copy")
    private String businessLicenseCopy;
 
    @JSONField(name = "business_license_number")
    private String businessLicenseNumber;
 
    @JSONField(name = "merchant_name")
    private String merchantName;
 
    @JSONField(name = "legal_person")
    private String legalPerson;
 
    @JSONField(name = "company_address")
    private String companyAddress;
 
    @JSONField(name = "business_time")
    private String businessTime;
 
    public LkWeChatBusinessLicenseInfo(){
        this.businessLicenseCopy = "1";
        this.businessLicenseNumber = "2";
        this.merchantName = "3";
        this.legalPerson = "4";
        this.companyAddress = "5";
        this.businessTime = "6";
    }
}

4.加上注解后打印转换后的json

System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));

{
    "business_license_copy":"1",
    "business_license_number":"2",
    "business_time":"6",
    "company_address":"5",
    "legal_person":"4",
    "merchant_name":"3"
}

三、字段排序

1.我们输出打印的json是这样的

{
    "business_license_copy":"1",
    "business_license_number":"2",
    "business_time":"6",
    "company_address":"5",
    "legal_person":"4",
    "merchant_name":"3"
}

我们想按照一定的顺序重新排序key

2.在@JSONField注解加上排序ordinal

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import java.io.Serializable;
 

@Data
public class LkWeChatBusinessLicenseInfo implements Serializable {
 
    private static final long serialVersionUID = 1582941630439552458L;
 
    @JSONField(name = "business_license_copy",ordinal = 1)
    private String businessLicenseCopy;
 
    @JSONField(name = "business_license_number",ordinal = 2)
    private String businessLicenseNumber;
 
    @JSONField(name = "merchant_name",ordinal = 3)
    private String merchantName;
 
    @JSONField(name = "legal_person",ordinal = 4)
    private String legalPerson;
 
    @JSONField(name = "company_address",ordinal = 5)
    private String companyAddress;
 
    @JSONField(name = "business_time",ordinal = 6)
    private String businessTime;
 
    public LkWeChatBusinessLicenseInfo(){
        this.businessLicenseCopy = "1";
        this.businessLicenseNumber = "2";
        this.merchantName = "3";
        this.legalPerson = "4";
        this.companyAddress = "5";
        this.businessTime = "6";
    }
}

3.输出打印转换后的实体:

System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));
{
    "business_license_copy":"1",
    "business_license_number":"2",
    "merchant_name":"3",
    "legal_person":"4",
    "company_address":"5",
    "business_time":"6"
}

小结:重命名除@JSONField,还有@JsonProperty、@SerializedName;@JsonProperty主要用于入参转换,和Json字符串序列化为Java对象;@SerializedName 改变了默认序列化和默认反序列化的字段取值;

@JSONField注解常用的使用场景

应用场景:

当我们在与前端进行交互时,前端想要的字段与我们提供的字段名不同,这时候一种解决方案是修改实体类,但如果该实体类应用的比较多,那改起来的代价太大,因此,可以使用注解@JSONField来实现替换效果,用法如下:

@JSONField(name = "size_new")
private int size;

一、JSON内容与实体类,@JSONField常规写法

JSON(与下述JSON字符串内容一致)

{
    size: 5,
    weight: 10,
    colour: "red"
}

实体类(AppleDO.java)

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
public class AppleDO {
    @JSONField(name = "size_new")
    private int size;
    @JSONField(name = "weight_new")
    private int weight;
    @JSONField(name = "colour_new")
    private String colour;
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    public String getColour() {
        return colour;
    }
    public void setColour(String colour) {
        this.colour = colour;
    }
}

二、JSON字符串转对应Java对象

执行代码

public static void main(String[] args) {
    String json = "{\n" +
        "    size_new: 5,\n" +
        "    weight_new: 10,\n" +
        "    colour_new: \"red\",\n" +
        "}";
    AppleDO appleDO = JSON.parseObject(json, AppleDO.class);
    System.out.println(appleDO.getSize());
    System.out.println(appleDO.getWeight());
    System.out.println(appleDO.getColour());
}

运行结果

三、支持序列化和反序列化

源码中的序列化和反序列化默认值均为true,则默认情况下是允许该字段序列化和反序列化的,如下:

boolean serialize() default true;
boolean deserialize() default true;

使用方法(以下不支持序列化,支持反序列化)

@JSONField(name = "size_new", serialize = false, deserialize = true)
private int size;

当我们的某些字段为空值时,我们仍希望将此字段返回到前端(该配置可以返回带有空字段的字符串,但是当字段为基本数据类型时无效,须将其转换为包装类)

@JSONField(serialzeFeatures= SerializerFeature.WriteMapNullValue)

四、指定字段顺序

将Java对象转换为JSON格式,转换后的字段顺序会根据首字母来排序,亦可通过如下方式来指定字段顺序:

@JSONField(name = "size_new", ordinal = 3)
private int size;
@JSONField(name = "weight_new", ordinal = 1)
private int weight;
@JSONField(name = "colour_new", ordinal = 2)
private String colour;

执行代码

AppleDO apple = new AppleDO();
apple.setSize(6);
apple.setWeight(12);
apple.setColour("green");
String appleStr = JSON.toJSONString(apple);
System.out.println(appleStr);

加ordinal参数之前运行结果


加ordinal参数之后运行结果

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Java对象转Json,关于@JSONField对象字段重命名和顺序问题

本文链接: https://lsjlt.com/news/166334.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作