Jersey JSON 键/值顺序

发布于 2024-11-19 23:44:06 字数 1679 浏览 3 评论 0原文

我正在使用 Jersey 构建一个 Java REST Web 应用程序来获取 JSON 数据输出。

我遇到了键/值对排序的问题。 我希望它是这样的顺序:id,name,areaId 但在我的 JSON 输出中,它的顺序是:areaId, id, name

有人知道如何控制顺序吗?


这是我的房间对象类:

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class RoomEntitiy {

private int id;
private String name;
private int areaId;

public RoomEntitiy() {}

public RoomEntitiy(int id, String name, int areaId) {
    this.id = id;
    this.name = name;
    this.areaId = areaId;
}
@XmlAttribute
public final int getId() {
    return id;
}

public final void setId(int id) {
    this.id = id;
}

public final String getName() {
    return name;
}

public final void setName(String name) {
    this.name = name;
}


public final int getArea() {
    return areaId;
}

public final void setArea(int areaId) {
    this.areaId = areaId;
}

}

这是我的 JSON 输出:

{
"rooms": [
    {
        "areaId": 2,
        "id": 1,
        "name": "room 831"
    },
    {
        "areaId": 1,
        "id": 2,
        "name": "room 683"
    },
    {
        "areaId": 1,
        "id": 3,
        "name": "Raum 485"
    },
    {
        "areaId": 5,
        "id": 4,
        "name": "room 600"
    },
    {
        "areaId": 2,
        "id": 5,
        "name": "room 283"
    },
    {
        "areaId": 4,
        "id": 6,
        "name": "room 696"
    }
  ]
}

更新 好的,我解决了。感谢您的回复。

我刚刚在 @XmlRootElement 下面添加了以下代码

@XmlType(propOrder = {
    "id",
    "name",
    "areaId"
})

I'm building a Java REST Web App with use of Jersey to get JSON data output.

I ran into a problem with ordering of the key/value pairs.
I want it to be this order: id, name, areaId
But in my JSON ouput it is this order: areaId, id, name though.

Does someone know how to control the ordering?


This is my Room Object Class:

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class RoomEntitiy {

private int id;
private String name;
private int areaId;

public RoomEntitiy() {}

public RoomEntitiy(int id, String name, int areaId) {
    this.id = id;
    this.name = name;
    this.areaId = areaId;
}
@XmlAttribute
public final int getId() {
    return id;
}

public final void setId(int id) {
    this.id = id;
}

public final String getName() {
    return name;
}

public final void setName(String name) {
    this.name = name;
}


public final int getArea() {
    return areaId;
}

public final void setArea(int areaId) {
    this.areaId = areaId;
}

}

This is my JSON output:

{
"rooms": [
    {
        "areaId": 2,
        "id": 1,
        "name": "room 831"
    },
    {
        "areaId": 1,
        "id": 2,
        "name": "room 683"
    },
    {
        "areaId": 1,
        "id": 3,
        "name": "Raum 485"
    },
    {
        "areaId": 5,
        "id": 4,
        "name": "room 600"
    },
    {
        "areaId": 2,
        "id": 5,
        "name": "room 283"
    },
    {
        "areaId": 4,
        "id": 6,
        "name": "room 696"
    }
  ]
}

UPDATE
Ok, I solved it. Thanks for your replies.

I just added the following code below @XmlRootElement

@XmlType(propOrder = {
    "id",
    "name",
    "areaId"
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空气里的味道 2024-11-26 23:44:06

看来元素的顺序是按字母顺序排列的。(是的,我是夏洛克·福尔摩斯..)。在大多数情况下,不需要对各个属性进行排序。但我相信我曾经做过一些定制,你可以使用以下注释来控制序列化。

@XmlRootElement
@XmlType(propOrder={"id", "areaId", "name"})

如果这不起作用,那么您可以使用其他序列化机制,例如 Jackson 或 Gson。

It seems that the order of the element is Alphabetical.(Yes, I am Sherlock Holmes..). In most cases there is no need to order individual attributes. But I believe I had once done some customization and you can use the following annotation to control serialization.

@XmlRootElement
@XmlType(propOrder={"id", "areaId", "name"})

If this does not work, then you could use other serialization mechanism like Jackson or Gson.

人│生佛魔见 2024-11-26 23:44:06

JSON 对象显式无序

对象是零个或多个名称/值对的无序集合

如果您想要顺序,请使用数组(但在这种情况下,无论您使用什么来处理数据都应该只按照它想要的顺序请求键而不是循环)。

JSON objects are explicitly unordered.

An object is an unordered collection of zero or more name/value pairs

If you want order, use an array (but in this case, whatever you use to process the data should just request the keys in the order it wants instead of looping).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文