java - 按用户 ID 对 json 文件中的消息进行分组

标签 java json

我能够读取包含消息列表及其相应作者的 JSON 文件。单个消息的结构如下:

JSON

{
"created_at": "Thu Apr 30 10:47:49 +0000 2015",
"id": 593728455901990900,
"user": {
    "id": 12,
    "name": "GiGi",
    "user_date": "Thu May 17 10:47:49 +0000 2010"
}

}

Author 和 Message 类 (POJO) 包含我们要解析的字段以及一个以字符串形式显示字段的函数 toString()

作者

public class Author {

    @SerializedName("id")
    private static long id;

    @SerializedName("created_at")
    private static String user_date;

    private static String name;

    public  String toString() {
        return name + "\n" + id + "\n" + user_date;         
    }
    ....Getters & Setters....
}

留言

public class Message {

    @SerializedName("user")
    Author author;   

    @SerializedName("created_at")
    String date;

    long id;

    public String toString() {
        return id + "\n" + date;        
    }

}

作者是消息类的成员类,我需要显示按用户分组的消息,但我似乎找不到一种优雅的方法。

更新

解决方案是创建一个 Map,将作者的属性之一作为 KEY(在此代码示例中为作者的 id),并将相应的消息列表作为 VALUE。

Map<Long, List<Message>> map = new HashMap<Long, List<Message>>();
for (Message msg : listOfMessages) {      
    long key = msg.getAuthor().getId();

    if (map.get(key) == null) {
        map.put(key, new ArrayList<Message>());
    }
    map.get(key).add(msg);
}

此解决方案可能出现的问题:

  • 如何显示作者id姓名及其相应的消息列表?
  • 我不太确定这个解决方案是否优雅,因为 Message 类包含 Author 类,并且 MAP 值中“携带”了许多不必要的信息。

解决方案

在 Author 类中添加 equalshashCode 方法后,我们可以将 Author 实例映射为 Key。

@Override
public int hashCode() {
    int hash = 7;
    hash = 23 * hash + (int) (this.id ^ (this.id >>> 32));
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Author author = (Author) obj;
    if (this.id != author.id) {
        return false;
    }
    return true;
}

在主类中,我们可以迭代消息列表并正确填充 map :

Map<Author, List<Message>> map = new HashMap<Author, List<Message>>();
for (Message message : listOfMessages) {
    List<Message> userMessages = map.get(message.getAuthor());
    if (userMessages == null) {
        userMessages = new ArrayList<Message>();
        map.put(message.getAuthor(), userMessages);
    }
    userMessages.add(message);
}
for (Entry<Author, List<Message>> entry : map.entrySet()) {
    System.out.println(entry.getKey().getId());
    for (Message message : entry.getValue()) {
        System.out.println("\t" + message.getDate());
    }
}

输出

3037057186
    Thu Apr 30 10:47:52 +0000 2015
1598532858
    Thu Apr 30 10:47:51 +0000 2015
67267979
    Thu Apr 30 11:47:49 +0000 2015
    Thu Apr 30 10:47:49 +0000 2015
    Thu Apr 30 10:47:49 +0000 2015
    Thu Apr 30 10:47:49 +0000 2015

最佳答案

考虑这个 json 文件 example.json

 [
    { 
     "date":"Thu Apr 30 10:47:49 +0000 2015",
     "id":593728455901990912,
     "author":{  
           "id":12,
            "name":"GiGi",
            "user_date":"Thu May 17 10:47:49 +0000 2010"
            } 
    },

    { 
     "date":"Thu Apr 30 10:47:49 +0000 2013",
     "id":593728455901990977,
     "author":{  
           "id":12,
            "name":"GiGi",
            "user_date":"Thu May 17 10:47:49 +0000 2010"
            } 
    },

    {
      "date":"Thu Apr 30 10:47:49 +0000 2015",
      "id":593728422901330999,
      "author":{  
            "id":13,
             "name":"HiHi",
             "user_date":"Thu May 17 10:47:49 +0000 2015"
              }
    }
 ]

作者类别

class Author {    
    private long id;
    private String name;
    private String user_date;
    public String toString() {
        return name + " " + id + " " + user_date;
    }    
   //getters, setters,...   
}

消息类别

class Message {
    String date;
    long id;
    Author author;
    public String toString() {
        return id + " " + date;
    }
   //getters, setters,...
}

使用 Gson API 解析 json

Gson gson = new Gson();
List<Message> list = gson.fromJson(new BufferedReader(new FileReader(
            "example.json")), new TypeToken<List<Message>>() {
}.getType());

显示按用户 ID 分组的消息

Map<Long, List<Message>> groupedMap = list.stream().collect(
            Collectors.groupingBy(m->  m.getAuthor().getId()));

groupedMap.forEach((k, v) -> System.out.println(k + " => " + v));

输出:

12 => [593728455901990912 Thu Apr 30 10:47:49 +0000 2015, 593728455901990977 Thu Apr 30 10:47:49 +0000 2013]
13 => [593728422901330999 Thu Apr 30 10:47:49 +0000 2015]

关于java - 按用户 ID 对 json 文件中的消息进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30082103/

相关文章:

javascript - Node js 服务器 : I want to respond to a browser request with both HTML and JSON in the same response, 这可能吗?

java - 从 Google 图书 api 获取缩略图

mysql - 显示从 mysql 数据库到 laravel Blade View 的 json 数组数据

ios - 无法将保存的 JSON 数据加载到 View 中

json - 使用 Redis 进行 Spring Boot 缓存 - 反序列化问题

java - 网页抓取 Java 初学者

java - 从 Nashorn 获取扩展 Java 类

java - Lucee Java CPU 使用率突然升高

java - 逐行写入文件

java - BroadcastReceiver 仅触发一次