来自JSON的POJO表示

时间:2017-04-12 13:08:06

标签: java json 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是什么样的?

PS。这是json的例子

{&#34; text&#34;:&#34; Flood / Storm / Tree Down。北部海滩(King Rd,Ingleside,NSW 2101)于2017年3月6日21:38,&#34;用户&#34;:{&#34; id&#34;:&#34; 4721717942&#34;,&#34 ;姓名&#34;:&#34;新南威尔士州消防更新&#34;},&#34; lang&#34;:&#34; en&#34;,&#34;坐标&#34;:{&#34;坐标&#34;:[151.264811,-33.6848],&#34;输入&#34;:&#34;点&#34;},&#34; created_at&#34;:&#34; Mon Mar 06 10:44 :31 +0000 2017&#34;},...,...

1 个答案:

答案 0 :(得分:0)

以Gson为例。

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