-
Notifications
You must be signed in to change notification settings - Fork 452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[QUESTION]JSONObject.toJSONString 序列化乱序,没有按照对象属性的顺序来显示 #2443
Labels
question
Further information is requested
Comments
JSONWriter.Feature.SortMapEntriesByKeys 看下这个 |
不行,加了这个还是乱序 我的key都是中文,使用了@JSONField做了中文别名配置,和这个是否有关系?? |
能帮忙构建一个重现问题的testcase么? |
public class Test {
@Getter
@Setter
static class User{
@JSONField(name = "登录名")
String loginName = "";
@JSONField(name = "用户名")
String userName = "";
@JSONField(name = "年龄")
String userAge = "";
@JSONField(name = "性别")
String userSex = "";
@JSONField(name = "出生日期")
String userBithday = "";
}
public static void main(String[] args) {
User user = new User();
System.out.println(JSON.toJSONString(user));
//乱序了
//{"出生日期":"","年龄":"","性别":"","用户名":"","登录名":""}
//我想要的是按照类的属性顺序显示
//{"登录名":"","用户名":"","年龄":"","性别":"","出生日期":""}
}
} |
可以使用JSONType#orders来指定顺序 @Test
public void test() {
User user = new User();
assertEquals("{\"登录名\":\"\",\"用户名\":\"\",\"年龄\":\"\",\"性别\":\"\",\"出生日期\":\"\"}", JSON.toJSONString(user));
}
@JSONType(orders = {"登录名", "用户名", "年龄", "性别", "出生日期"})
@Getter
@Setter
static class User {
@JSONField(name = "登录名")
String loginName = "";
@JSONField(name = "用户名")
String userName = "";
@JSONField(name = "年龄")
String userAge = "";
@JSONField(name = "性别")
String userSex = "";
@JSONField(name = "出生日期")
String userBithday = "";
} |
有没有更方便的处理方式,我有一百多个属性 |
也可以在JSONField#ordinal中指定输出顺序 @Test
public void test1() {
User1 user = new User1();
assertEquals(
"{\"登录名\":\"\",\"用户名\":\"\",\"年龄\":\"\",\"性别\":\"\",\"出生日期\":\"\"}",
JSON.toJSONString(user));
}
@Getter
@Setter
static class User1 {
@JSONField(name = "登录名", ordinal = 1)
String loginName = "";
@JSONField(name = "用户名", ordinal = 2)
String userName = "";
@JSONField(name = "年龄", ordinal = 3)
String userAge = "";
@JSONField(name = "性别", ordinal = 4)
String userSex = "";
@JSONField(name = "出生日期", ordinal = 5)
String userBithday = "";
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
现在有个需求页面展示的顺序和对象属性顺序一致,但是JSONObject.toJSONString后乱序了,原有的SerializerFeature.SortField被移除了,目前这个版本我该如何配置??
The text was updated successfully, but these errors were encountered: