简体   繁体   English

我们如何使用 Java 更改 JSON 对象的顺序?

[英]How can we change the order of JSON Object using Java?

I am pretty new to JSON and trying to construct a new JSON request file using Java.我对 JSON 非常陌生,并尝试使用 Java 构建一个新的 JSON 请求文件。 Here is what I need to achieve这是我需要实现的目标

{
  "ID": "9724234234",
  "Details": [
    {
      "Name": "Donny",
      "EmpID": "B572345",
      "country": "India",
      }
   ]
}

Here is my Java code这是我的 Java 代码

public static void main(String[] args) {
    JSONObject obb = new JSONObject();
    obb.put("ID", "9724234234");

    JSONArray jsonArray = new JSONArray();
    obb.putAll(obb);
    JSONObject obj = new JSONObject();
    obj.put("Name", "Donny");
    obj.put("EmpID", "B572345");
    obj.put("Country", "India");

    jsonArray.add(obj);

    JSONObject obj1 = new JSONObject();
    obj1.put("Details", jsonArray);

    obb.putAll(obj1);
    System.out.println(obb);
}

What I am getting the JSON as我得到的 JSON 作为

{
  "Details": [
    {
      "Country": "India",
      "EmpID": "B572345",
      "Name": "Donny"
    }
  ],
  "ID": "9724234234"
}

As you can see, my ID is coming in the incorrect position of the JSON, I want it to come in the first part of JSON.如您所见,我的 ID 出现在 JSON 的错误位置,我希望它出现在 JSON 的第一部分。 Am I missing anything?我错过了什么吗? Any help is appreciated.任何帮助表示赞赏。 Cheers.干杯。

You cannot specify the order of keys in JSON, and it does not matter.您不能在 JSON 中指定键的顺序,这无关紧要。 Any code that attempted for some reason to rely on the ordering of the keys would fail.任何出于某种原因尝试依赖键顺序的代码都将失败。 This is the case with most standard hashmap/key-value pair implementations, and in some languages (eg Go), the ordering is deliberately randomised to ensure no-one tries to rely on it.大多数标准哈希映射/键值对实现都是这种情况,并且在某些语言(例如 Go)中,排序是故意随机化的,以确保没有人试图依赖它。

You can't rely on the ordering of elements within a JSON object.您不能依赖 JSON 对象中元素的顺序。 JSON libraries are free to rearrange the order of the elements as they see fit. JSON 库可以自由地重新排列元素的顺序,因为它们认为合适。 its not a bug.它不是一个错误。

more information abou json in json.org有关json.org 中json 的更多信息

First of all the ordering of the Json elements hardly matter it can only be eye candy not more than that, however give a try with首先,Json 元素的顺序几乎无关紧要,它只能令人赏心悦目,但请尝试一下

public static void main(String[] args) {
JSONObject obb = new JSONObject();


JSONArray jsonArray = new JSONArray();

JSONObject obj = new JSONObject();
obj.put("Name", "Donny");
obj.put("EmpID", "B572345");
obj.put("Country", "India");

jsonArray.add(obj);
obb.put("ID", "9724234234");
obb.putAll(obb);
JSONObject obj1 = new JSONObject();
obj1.put("Details", jsonArray);

obb.putAll(obj1);
System.out.println(obb);

} }

Use @JsonPropertyOrder annotation on class level and provide the order in which you want your JSON to be created.在类级别使用 @JsonPropertyOrder 注释并提供您希望创建 JSON 的顺序。 This will only help you if you are using jackson library for your JSON parsing.如果您使用 jackson 库进行 JSON 解析,这只会对您有所帮助。

eg.例如。

@JsonPropertyOrder({"name", "id"})
public class Test{
    private int id;
    private String name;
    
    getter/setter
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM