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

时间:2015-02-20 09:27:00

标签: 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":[
         { "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
}]}

2 个答案:

答案 0 :(得分:0)

我试过这样但我无法搜索和排序元素。 private static final String filePath =“E:\ List.json”;

public static void main(String[] args) {

    try {

        FileReader reader = new FileReader(filePath);

        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);


        JSONArray lang= (JSONArray) jsonObject.get("Hello");

        Map<JSONObject, String> jsonValues = new TreeMap<JSONObject, String>();

        for(int i=0; i<lang.size(); i++){
            jsonValues.putAll((JSONObject) lang.get(i));
            System.out.println(jsonValues.toString());

        }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParseException ex) {
            ex.printStackTrace();
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }

    }

答案 1 :(得分:0)

首先,可能最好的方法是定义包含json数据的模型类。您可以在以后轻松操作该数据,并且您的代码将更加清洁&#34;总的来说。

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);
}

然后,您可以使用Collections类对日期列表中的数据进行排序,例如:像:

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

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