2017-04-12 63 views
0

首先原諒我,因爲我認爲這是一個愚蠢的問題。我剛開始學習javaJSON的POJO表示

我有我被解析到POJO json文件,這樣我可以在他們做業務

這是解析器實現

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 = [email protected], 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 = [email protected], coordinates = null], ..., ..., ...]]] 

那麼實際的POJO是什麼樣的?

PS。這是示例json

{「text」:「Flood/Storm/Tree Down。Northern Beaches(King Rd,Ingleside,NSW 2101)於2017年3月6日21:38,」user「:{」id「 「4721717942」,「name」:「NSW Fire Updates」},「lang」:「en」,「coordinates」:{「coordinates」:[151.264811,-33.6848],「type」:「Point」},「created_at 「: 「星期一3月6 10點44分31秒+0000 2017年」},...,...

+0

普通Java對象的教練可能要你do marshaling – efekctive

+0

你認爲每一行都是一個單獨的json嗎?這看起來很有道理。但是,如果沒關係,那麼你的tweets列表就是o k並且確實只包含簡單的java結構對象。 –

+0

我一行一行解析它。我的教師說,所有的POJO都存儲在List 推文中,但我也打印了System.out.println(推文),那麼這怎麼可能不是實際的POJO?該任務僅僅是解析。我沒必要真的打印它,它不是一個要求。但我只是好奇它實際上看起來像 – Kutam

回答

0

使用GSON例如

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