如何使用java对json数组进行排序和搜索

如何使用java对json数组进行排序和搜索,java,json,Java,Json,我的Json文件如下 { "Hello" : [{ "CommentId":28227, "Comments":"User Philip John has added a new file", "DisplayComments":"User <a id='getUser' data='205' >Philip John</a> has added a new file.", "Users":[ { "Use

我的Json文件如下

{
"Hello" : [{
      "CommentId":28227,
      "Comments":"User Philip John has added a new file",
      "DisplayComments":"User <a id='getUser' data='205' >Philip John</a> has added a new file.",
      "Users":[
         { "UserId":"1", "UserName":"User one" },
         { "UserId":"2", "UserName":"User two" }
      ]
},{
      "CommentId":28226,
      "Comments":"docs",
      "DisplayComments":"docs",
      "RefCommentId":28226,
      "IsSystemGenerated":0,
      "CommentDate":"2014-09-17T06:51:47.317",
      "Users": [
         {"UserId":"1","UserName":"User one"},
         {"UserId":"3","UserName":"User Three"}
         ]
},{
      "CommentId":28225,
      "Comments":"New Group aa has been created.",
      "DisplayComments":"New Group <a id='getGroup' data='88' >aa</a> has been created.",
      "RefCommentId":28225,
      "IsSystemGenerated":1,
      "CommentDate":"2014-09-16T07:21:38.493",
      "Users":[
         {"UserId":"3","UserName":"User three"},
         {"UserId":"4","UserName":"User four" }
      ]
},{
      "CommentId":28224,
      "Comments":"New Group Philip has been created.",
      "DisplayComments":"New Group <a id='getGroup' data='87' >ss</a> has been created.",
      "RefCommentId":28224,
      "IsSystemGenerated":1,
      "CommentDate":"2014-09-16T06:00:58.897",
      "Users":null
}]}
{
“你好”:[{
“CommentId”:28227,
“评论”:“用户Philip John添加了一个新文件”,
“DisplayComments”:“用户Philip John已添加新文件。”,
“用户”:[
{“UserId”:“1”,“UserName”:“User one”},
{“UserId”:“2”,“UserName”:“User two”}
]
},{
“CommentId”:28226,
“评论”:“文件”,
“显示评论”:“文档”,
“参考注释ID”:28226,
“IsSystemGenerated”:0,
“评论日期”:“2014-09-17T06:51:47.317”,
“用户”:[
{“UserId”:“1”,“UserName”:“User one”},
{“UserId”:“3”,“UserName”:“User Three”}
]
},{
“CommentId”:28225,
“评论”:“新的aa组已创建。”,
“DisplayComments”:“新的aa组已创建。”,
“参考注释ID”:28225,
“IsSystemGenerated”:1,
“评论日期”:“2014-09-16T07:21:38.493”,
“用户”:[
{“UserId”:“3”,“UserName”:“User three”},
{“UserId”:“4”,“UserName”:“User four”}
]
},{
“CommentId”:28224,
“评论”:“新集团Philip已创建。”,
“DisplayComments”:“新的组ss已创建。”,
“参考注释ID”:28224,
“IsSystemGenerated”:1,
“评论日期”:“2014-09-16T06:00:58.897”,
“用户”:空
}]}

我试过这样做,但无法搜索和排序元素。 私有静态最终字符串filePath=“E:\List.json”

publicstaticvoidmain(字符串[]args){
试一试{
FileReader=新的FileReader(filePath);
JSONParser JSONParser=新的JSONParser();
JSONObject JSONObject=(JSONObject)jsonParser.parse(reader);
JSONArray lang=(JSONArray)jsonObject.get(“Hello”);
Map jsonValues=newtreemap();

对于(inti=0;i首先,最好是定义包含json数据的模型类。这样以后,您就可以轻松地使用这些数据,而且您的代码通常也会更“干净”

public class Hello{
    private String commentId;
    private String comments;
    private Date commentDate;

    // ... other fields
    // getters and setters...
}
在您自己的回答中,您发布了带有JSONParser和JSONObject类的示例。然后您可以使用这些类将数据填充到模型类中

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
JSONArray lang= (JSONArray) jsonObject.get("Hello");

List<Hello> objs = new ArrayList<Hello>();

for (int i = 0; i < lang.length(); i++) {
    JSONObject rec = lang.getJSONObject(i);
    String commentId = rec.getInt("CommentId");
    String comments = rec.getString("Comments");
    // ...

    Hello h = new Hello();
    h.setCommentId(commentId);
    h.setComments(comments);
    // ...

    // store you objects into list
    objs.add(h);
}
JSONParser-JSONParser=new-JSONParser();
JSONObject JSONObject=(JSONObject)jsonParser.parse(reader);
JSONArray lang=(JSONArray)jsonObject.get(“Hello”);
List objs=new ArrayList();
for(int i=0;i
然后,您可以使用Collections类按日期对列表中的数据进行排序,例如:

Collections.sort(objs, new Comparator<Hello>() {

    @Override
    public int compare(Hello o1, Hello o2) {
        return o1.getCommentDate().compareTo(o2.getCommentDate());
    }
});
Collections.sort(objs,新的Comparator(){
@凌驾
公共整数比较(Hello o1,Hello o2){
返回o1.getCommentDate().compareTo(o2.getCommentDate());
}
});

使用Boon如何对JsonArray进行排序,或者是否有任何其他流程来排序和搜索Json数据。
Collections.sort(objs, new Comparator<Hello>() {

    @Override
    public int compare(Hello o1, Hello o2) {
        return o1.getCommentDate().compareTo(o2.getCommentDate());
    }
});