如何使用gson创建按键排序的json? [英] How to create json, sorted on keys, using gson?

查看:251
本文介绍了如何使用gson创建按键排序的json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建常量json字符串或按键排序的json.常量json字符串是什么意思?请查看下面创建的代码示例.

I need to create constant json string or a json sorted on keys. What do I mean by constant json string? Please look into following code sample, which I created.

我的代码1:

public class GsonTest
{
    class DataObject {

        private int data1 = 100;
        private String data2 = "hello";

    }   


    public static void main(String[] args)
    {
        GsonTest obj=new GsonTest();
        DataObject obj2 = obj.new DataObject();
        Gson gson = new Gson();

        String json = gson.toJson(obj2);
        System.out.println(json);
    }
}

输出1:

{"data1":100,"data2":"hello"}

我的代码2:

public class GsonTest
{
    class DataObject {
        private String data2 = "hello";
        private int data1 = 100;


    }   


    public static void main(String[] args)
    {
        GsonTest obj=new GsonTest();
        DataObject obj2 = obj.new DataObject();
        Gson gson = new Gson();

        String json = gson.toJson(obj2);
        System.out.println(json);
    }
}

输出2:

{"data2":"hello","data1":100}

如果看到的话,如果我切换变量(DataObject类中的data1& data2),则会得到不同的json.我的目标是获得相同的json,即使有人更改类变量的位置也是如此.当有人添加新变量时,我会理解它,而json会发生变化.但是当变量移动时,json不应更改.因此,我的目标是获取标准json,可能以相同类的键顺序排序.如果存在嵌套的json,则应在嵌套结构中对其进行排序.

If you see, if I switch variables (data1 & data2 in DataObject class), I get different json. My objective to get same json, even if somebody changes position of the class variables. I get it when somebody adds new variables, json would change. But json shouldn't change when variables are moved around. So, my objective is to get standard json, possibly in sorted keys order for same class. If there is nested json, then it should be sorted in the nested structure.

两种代码在运行时的预期输出:

{"data1":100,"data2":"hello"}  //sorted on keys!! Here keys are data1 & data2

我了解,我需要在String json = gson.toJson(obj2);行中进行一些更改,但是我该怎么办?

I understand, I need to change something in String json = gson.toJson(obj2); line, but what do I have to do?

为什么需要订购它们?

我需要编码json字符串,然后将其传递给另一个函数.如果更改键的顺序,即使值保持不变,编码后的值也会改变.我想避免这种情况.

I need to encode the json string and then pass it to another function. If I change the order of keys, even though value remain intact, the encoded value will change. I want to avoid that.

推荐答案

首先,根据定义,json对象的键为无序,请参见

First of all, the keys of a json object are unordered by definition, see http://json.org/.

如果您只想要带有顺序键的json字符串,则可以尝试将json反序列化为排序的映射,然后序列化该映射以获得按密钥排序的json字符串.

If you merely want a json string with ordered keys, you can try deserializing your json into a sorted map, and then serialize the map in order to get the sorted-by-key json string.

GsonTest obj=new GsonTest();
DataObject obj2 = new DataObject();
Gson gson = new Gson();

String json = gson.toJson(obj2);
TreeMap<String, Object> map = gson.fromJson(json, TreeMap.class);
String sortedJson = gson.toJson(map);

这篇关于如何使用gson创建按键排序的json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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