将 JSON 字符串转换为 Java 中的 JSON 对象。

huangapple go评论77阅读模式
英文:

Converting json string to json object as it is in Java

问题

我想将JSON字符串转换为具有其顺序的JSON对象。
我的代码:

String responseData = "{\"contents\": [{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\"}], \"maxsize\": 5, \"startIndex\": 1, \"status\": \"suspended\", \"activated\": false}";
JSONObject jsonObject = new JSONObject(responseData);
System.out.println(jsonObject.toString());

我使用了org.json版本-20200518库,但当我尝试时,我得到了以下输出:

{"startIndex":1,"contents":[{"a":"A","b":"B","c":"C"}],"maxsize":5,"status":"suspended","activated":false}

但我想要以下输出:

{"contents": [{"a": "A", "b": "B", "c": "C"}], "maxsize": 5, "startIndex": 1, "status": "suspended", "activated": false}

任何帮助都将不胜感激!

英文:

I want to convert json string to json object with it's order.
My code:

String responseData = "{ \"contents\": [ { \"a\" : \"A\" , \"b\": \"B\", \"c\" : \"C\" } ] , \"maxsize\" : 5, \"startIndex\" : 1, \"status\" : \"suspended\", \"activated\" : false}"
JSONObject jsonObject = new JSONObject(responseData);
System.out.println(jsonObject.toString());

I used org.json version - 20200518 library and when I tried , I got following output:

{"startIndex":1,"contents":[{"a":"A","b":"B","c":"C"}],"maxsize":5,"status":"suspended","activated":false}

but I want below output:

{ "contents" : [ { "a" : "A" , "b": "B", "c" : "C" } ] , "maxsize" : 5, "startIndex" : 1, "status" : "suspended", "activated" : false}

Any help is much appreciated!

答案1

得分: 1

org.json使用HashMap来存储解析后的数据。因此,无法保证顺序。

理想情况下,元素的顺序不应该影响。但是,如果您必须确实需要顺序,并且可以更改您的JSON库,那么请使用fasterxml-jackson。它使用LinkedHashMap并保留顺序。

还要注意,org.json是一个非常基本且轻量级(66KB)的库,可以完成任务。Jackson至少为1.7MB以上。

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

public class JsonTest {

    String responseData = "{'contents': [{'a': 'A', 'b': 'B', 'c': 'C'}], 'maxsize': 5, 'startIndex': 1, 'status': 'suspended', 'activated': false}";

    @Test
    void orgJson() {
        JSONObject jsonObject = new JSONObject(responseData);
        System.out.println(jsonObject.toString()); //无序
    }

    @Test
    void jackson() throws JsonProcessingException {
        JsonNode jsonObject = new ObjectMapper().readTree(responseData);
        System.out.println(jsonObject.toString()); //保持顺序
    }
}
英文:

org.json uses HashMap to store the parsed data. So you cannot guarantee the order.

Ideally ordering of the elements should not matter. But if you must need that and can change your json library then use fasterxml-jackson. It uses LinkedHashMap and preserves the order.

Also note that org.json is very basic + lightweight(66KB) library that does the job. Jackson is at least 1.7MB+.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

public class JsonTest {

    String responseData = "{ \"contents\" : [ { \"a\" : \"A\" , \"b\": \"B\", \"c\" : \"C\" } ] , \"maxsize\" : 5, \"startIndex\" : 1, \"status\" : \"suspended\", \"activated\" : false}";

    @Test
    void orgJson() {
        JSONObject jsonObject = new JSONObject(responseData);
        System.out.println(jsonObject.toString()); //no ordering
    }

    @Test
    void jackson() throws JsonProcessingException {
        JsonNode jsonObject = new ObjectMapper().readTree(responseData);
        System.out.println(jsonObject.toString()); //keeps the ordering
    }
}

答案2

得分: 0

一个JSONObject本质上不是一个有序对象。因此,不幸的是,根据JSONObject类在Java中的工作方式,您所需要的是不可能的。您最好的排序方法是在对象内部传递一个指示排序的索引,然后按照该索引使用它。这里有一篇文章概述了如何实现这一点。

链接:https://www.tutorialspoint.com/how-can-we-sort-a-jsonobject-in-java

英文:

A JSONObject is not inherently an ordered object. So unfortunately what you require is not possible with how the JSONObject class works in Java. The best way for you to sort would be to pass an index within your object that would indicate sort and then use it as such. Here is an article outlining how that can be accomplished.

https://www.tutorialspoint.com/how-can-we-sort-a-jsonobject-in-java

huangapple
  • 本文由 发表于 2020年9月30日 22:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64139384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定