fastjson序列化,默认是用字母排序,
那么怎么来实现按照自己定义的顺序输出,想要的json串呢?
直接上代码:
package com.zpxinfu.test.testcase.interFace; import com.alibaba.fastjson.annotation.JSONType; //设置排序规则 @JSONType(orders = { "busiCode", "message"}) public class TransMessage { String busiCode; Message message; public String getBusiCode() { return busiCode; } public void setBusiCode(String busiCode) { this.busiCode = busiCode; } public Message getMessage() { return message; } public void setMessage(Message message) { this.message = message; } }
@JSONType与Serializable同时存在,jsontype的orders属性不起作用 to @JSONType中order未指明的字段,必须用@JSONField(serialize=false)修饰,否则排序无效
测试代码如下:
@Test public void getBody(){ List skill = new ArrayList(); skill.add("Java"); skill.add("IOS"); skill.add("Android"); User user = new User(); user.setName("SOJSON"); user.setAge(27); user.setSex("男"); user.setSkill(skill); String json = JSON.toJSONString(user); System.out.println(json); }
运行结果如下:
{"name":"SOJSON","age":27,"sex":"男","skill":["Java","IOS","Android"]}
时间: 2024-11-05 00:48:44