<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.66</version> </dependency>
import lombok.Data; import java.io.Serializable; /** * @类名 WeChatBusinessLicenseInfo * @描述 营业执照/登记证书信息(测试用) * @版本 1.0 * @创建人 XuKang * @创建时间 2021/12/24 10:43 **/ @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"; } }
System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));
"businessLicenseCopy":" 1","Nombor Lesen perniagaan":"2",
3 Kami ingin menukar kepada kunci bergaris, contohnya, untuk menukar businessLicenseCopy kepada business_license_copy
"Masa perniagaan":"6",
"Alamat syarikat":"5",
"Orang Sah":"4",
"Nama Pedagang" :"3"
}
Kami perlu mengubah suai entiti, menambah Anotasi @JSONField
4 Tambahkan anotasi dan cetak json yang ditukarimport com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; import java.io.Serializable; /** * @类名 WeChatBusinessLicenseInfo * @描述 营业执照/登记证书信息(测试用) * @版本 1.0 * @创建人 XuKang * @创建时间 2021/12/24 10:43 **/ @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"; } }
System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));
"business_license_copy":"1"," business_license_number":"2",
3. Pengisihan medan
"business_time":"6",
"company_address":"5",
"legal_person":"4",
"merchant_name":"3 "
}
"masa_niaga":"6",
"alamat_syarikat":"5","orang_undang-undang":"4",3. Output dan cetak entiti yang ditukar:"merchant_name":"3"
}
Kami mahu menyusun semula kunci dalam susunan tertentu
2. Tambahkan ordinal pengisihan
pada anotasi @JSONField
System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));
{<🎜_> "business_license ":"1",import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; import java.io.Serializable; /** * @类名 WeChatBusinessLicenseInfo * @描述 营业执照/登记证书信息(测试用) * @版本 1.0 * @创建人 XuKang * @创建时间 2021/12/24 10:43 **/ @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"; } }
"nama_pedagang":"3",
"orang_undang-undang":"4","alamat_syarikat":"5",<; 🎜>Senario aplikasi:"business_time":"6"
}
Ringkasan:
Selain @JSONField, nama semula juga termasuk @JsonProperty dan @JsonProperty terutamanya digunakan untuk penukaran parameter Input, dan pensirilan rentetan Json ke dalam objek Java; @SerializedName mengubah nilai medan penyirian lalai dan penyahsiran lalai
@JSONField senario penggunaan biasa
Apabila kami berinteraksi dengan bahagian hadapan, medan yang dikehendaki adalah berbeza daripada nama medan yang kami sediakan Pada masa ini, satu penyelesaian ialah mengubah suai entiti kelas, tetapi jika Kelas entiti ini digunakan secara meluas, jadi kos menukarnya terlalu tinggi Oleh itu, anda boleh menggunakan anotasi @JSONField untuk mencapai kesan penggantian adalah seperti berikut:
1. Kandungan JSON dan kelas entiti, @JSONField Penulisan konvensionalJSON (selaras dengan kandungan rentetan JSON berikut)
Kelas entiti (AppleDO.java)
@JSONField(name = "size_new") private int size;
kod pelaksanaan
{ size: 5, weight: 10, colour: "red" }
hasil larian
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; } }
3. Menyokong siri dan penyahsirilan
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()); }
Penggunaan kaedah (yang berikut tidak disokong Pensirian, penyahserikatan disokong)
boolean serialize() default true; boolean deserialize() default true;
Tukar objek Java kepada Format JSON, dan medan yang ditukar Pesanan akan diisih mengikut huruf pertama Anda juga boleh menentukan susunan medan dengan cara berikut:
@JSONField(name = "size_new", serialize = false, deserialize = true) private int size;
Laksanakan kod
@JSONField(serialzeFeatures= SerializerFeature.WriteMapNullValue)
@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;
Hasil larian selepas menambah parameter ordinal
AppleDO apple = new AppleDO(); apple.setSize(6); apple.setWeight(12); apple.setColour("green"); String appleStr = JSON.toJSONString(apple); System.out.println(appleStr);
Atas ialah kandungan terperinci Bagaimana untuk menyelesaikan masalah menamakan semula dan memesan medan objek @JSONField di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!