默认情况下,FlexJSON按字母顺序排序 [英] FlexJSON Orders Alphabetically by Default

查看:86
本文介绍了默认情况下,FlexJSON按字母顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将FlexJSON 2.1与Play Framework一起使用时,如何使用自定义顺序?现在我有:

How do I use a custom order when using FlexJSON 2.1 with Play Framework? Right now I have:

public String toJsonString() {
    // Include id & name, exclude all others.
    JSONSerializer ser = new JSONSerializer().include(
           "id",
           "firstName",
           "lastName",
           "email",
           "authToken",
           "dob",
           "home_zip",
           "gender",
    ).exclude("*").prettyPrint(true);
   return ser.serialize(this);
 }

它以字母顺序打印出来.

And it prints out in alphabetical order.

我发现了这篇 SO帖子,但我没有具有定义为@XMLRootElement的任何类. 如何告诉FlexJSON使用自定义排序?

I found this SO post, but I don't have any class defined as @XMLRootElement. How do I tell FlexJSON to use a custom ordering?

推荐答案

顺序仅从数据结构本身派生.采用顺序的数据结构(即List,TreeSet,TreeMap)将具有顺序.不保证对象内的字段或HashMap顺序.底线是,除了事物的自然顺序以外,没有明确地施加于事物的顺序.如果需要,您可以尝试覆盖默认的序列化变形金刚以强加顺序.您可以先将ObjectTransformer和MapTransformer子类化,然后尝试强加命令.

Order is only derived from the data structure itself. Data structures that employ an order (ie List, TreeSet, TreeMap) will have an order. Fields within objects or HashMap order isn't guaranteed. Bottom line there is no order explicitly imposed on things other than their natural order. You can try and override the default serialization Transformers to impose order if you want. You can start by subclassing ObjectTransformer and MapTransformer, and trying to impose an order.

对JSON数据结构进行排序似乎很奇怪,因为javascript中的对象内的字段也没有顺序.我无法想象为什么这样做很重要.

Ordering JSON data structures seems odd since fields within objects in javascript don't have an order either. I can't imagine why this would be important to do.

这篇关于默认情况下,FlexJSON按字母顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆