Building ordered JSON String from LinkedHashMap(从 LinkedHashMap 构建有序 JSON 字符串)
问题描述
我需要按插入顺序排列键/值对,因此我选择使用 LinkedHashMap
而不是 HashMap
.但我需要将 LinkedHashMap
转换为 JSON 字符串,其中 LinkedHashMap
中的顺序保留在字符串中.
I had a need for having Key/Value pairs in the order of my insertion, so I opted to use LinkedHashMap
over HashMap
. But I need to convert the LinkedHashMap
into a JSON String where the order in the LinkedHashMap
is maintained in the string.
但目前我正在通过以下方式实现它:
But currently I'm achieving it by:
- 首先将 LinkedHashMap 转换为 JSON.
然后将 JSON 转换成字符串.
- First converting the LinkedHashMap into JSON.
Then converting the JSON into a string.
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.JSONObject;
public class cdf {
public static void main(String[] args) {
Map<String,String > myLinkedHashMap = new LinkedHashMap<String, String>();
myLinkedHashMap.put("1","first");
myLinkedHashMap.put("2","second");
myLinkedHashMap.put("3","third");
JSONObject json = new JSONObject(myLinkedHashMap);
System.out.println(json.toString());
}
}
输出是:
{"3":"third","2":"second","1":"first"} .
但我希望它按插入键的顺序,如下所示:
But I want it in the order of insertion of the keys, like this:
{"1":"first","2":"second","3":"third"}
一旦我将 LinkedHashMap
转换为 JSON,它就会失去它的顺序(很明显 JSON 没有顺序的概念),因此字符串也是无序的.现在,如何生成与 LinkedHashMap
顺序相同的 JSON 字符串?
Once I convert the LinkedHashMap
into a JSON it loses it order (it's obvious that JSON doesn't have the notion of order) and hence the string too is out of order. Now, how do I generate a JSON string whose order is same as the LinkedHashMap
?
推荐答案
Gson 如果你的朋友.这会将有序的地图打印成有序的 JSON 字符串.
Gson if your friend. This will print the ordered map into an ordered JSON string.
如果要保留插入顺序,请使用 LinkedHashMap
.
If you want to preserve insertion order, use a LinkedHashMap
.
我使用的是最新版本的Gson(2.8.5),您可以通过本文底部的以下选项进行下载.
I used the latest version of Gson (2.8.5), you can can download it via the following options at the bottom of this post.
import java.util.*;
import com.google.gson.Gson;
public class OrderedJson {
public static void main(String[] args) {
// Create a new ordered map.
Map<String,String> myLinkedHashMap = new LinkedHashMap<String, String>();
// Add items, in-order, to the map.
myLinkedHashMap.put("1", "first");
myLinkedHashMap.put("2", "second");
myLinkedHashMap.put("3", "third");
// Instantiate a new Gson instance.
Gson gson = new Gson();
// Convert the ordered map into an ordered string.
String json = gson.toJson(myLinkedHashMap, LinkedHashMap.class);
// Print ordered string.
System.out.println(json); // {"1":"first","2":"second","3":"third"}
}
}
如果您希望项目始终插入正确的位置,请改用 TreeMap
.
If you want the items to always be inserted at the right place, use a TreeMap
instead.
import java.util.*;
import com.google.gson.Gson;
public class OrderedJson {
public static void main(String[] args) {
// Create a new ordered map.
Map<String,String> myTreeHashMap = new TreeMap<String, String>();
// Add items, in any order, to the map.
myTreeHashMap.put("3", "third");
myTreeHashMap.put("1", "first");
myTreeHashMap.put("2", "second");
// Instantiate a new Gson instance.
Gson gson = new Gson();
// Convert the ordered map into an ordered string.
String json = gson.toJson(myTreeHashMap, TreeMap.class);
// Print ordered string.
System.out.println(json); // {"1":"first","2":"second","3":"third"}
}
}
依赖选项
Maven
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
分级
compile 'com.google.code.gson:gson:2.8.5'
或者您可以访问 Maven 中心 获取更多下载选项.
Or you can visit Maven Central for more download options.
这篇关于从 LinkedHashMap 构建有序 JSON 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 LinkedHashMap 构建有序 JSON 字符串
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01