更改JSON数组对象的顺序

时间:2018-06-05 09:50:45

标签: java arrays json

我不确定这是否可行。假设我有一个类似于下面那个的JSON数组

   {    
    "hitsPerPage": 30,
    "hitList": {
        "Nodes": [
       {
            "Path": "/content/desktop/gt/en/personal/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",
        }, {
            "Path": "/content/desktop/gt/en/save-and-invest/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",       
        }, {
            "Path": "/content/desktop/gt/en/investment-accounts/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",
        }, {
            "Path": "/content/desktop/gt/en/tools-and-guidance/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",           
        }]
    }
}

是否可以重新排序显示对象节点数组的顺序,如

"Nodes": [
        {
            "Path": "/content/desktop/gt/en/investment-accounts/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",
        }, {
            "Path": "/content/desktop/gt/en/save-and-invest/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",       
        }, 
            "Path": "/content/desktop/gt/en/tools-and-guidance/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",           
        },{
            "Path": "/content/desktop/gt/en/personal/jcr:content",
            "Extension": "html",
            "Title": "jcr:content",
            "Type": "cq:PageContent",       
        }]

如果可能的话,使用Java中的任何示例代码

1 个答案:

答案 0 :(得分:0)

最简单的方法就是提取数据并以所需的格式重写json。 Google的Gson库非常适合将数据提取到java类并写入json。或者使用JsonObject / array / etc手动写入Json。 想想重组json是否必要,但很难理解为什么它会过于重要