有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自JSON的java POJO表示

首先请原谅我,我认为这是一个愚蠢的问题。我刚开始学java

我有一个json文件,我将其解析为POJO,以便对其进行操作

这是解析器实现

public class ParsingTweet {

public static void main(String[] args) throws IOException {
    assert args != null & args.length > 0;
    List<Tweet> tweets = new ArrayList<>();
    ObjectMapper mapper = new ObjectMapper();
    try (BufferedReader reader = new BufferedReader(new FileReader(args[0]))) {
        String line;
        while ((line = reader.readLine()) != null) {
            tweets.add(mapper.readValue(line, Tweet.class));
        }
    }
    System.out.println(tweets);
 }
}

但我的导师说这个结果不是实际的POJO。它只是一个字符串表示。比如

[[text = I think this would be good. Would hopefully light the fire in the later rounds if you knew you were losing..., created_at = Mon Mar 06 22:48:07 +0000 2017, user = User@56f4468b, coordinates = null], [text = @Night_0f_Fire He's stuck in the alimony machine,, bust him out, created_at = Mon Mar 06 22:47:53 +0000 2017, user = User@6cc4c815, coordinates = null], ..., ..., ...]]]

那么实际的POJO是什么样子的呢

以下是json的例子

2017年3月6日21:38,“文本”:“洪水/风暴/树木倒下。北部海滩(新南威尔士州英格尔赛德国王路2101号),“用户”:{“id”:“47217942”,“名称”:“新南威尔士州火灾更新”},“朗”:“恩”,“坐标”:{“坐标”:[151.264811,-33.6848],“类型”:“点”},“创建于”:“Mon Mar 06 10:44:31+0000 2017”}


共 (1) 个答案

  1. # 1 楼答案

    以Gson为例

    String fullFile = ...read file...;
    Gson gson = new Gson();
    List<Tweet> tweets = gson.fromJson(fullFile, new TypeToken<ArrayList<Tweet>>(){});
    System.out.println(tweets);