2017-04-11 53 views
0

到目前爲止,我有一個tweet.json文件,裏面是tweet的集合。我已經構建了一個tweet.class,它是tweet中鍵值的對象集合。使用Jackson的解析器實現

現在我想實現一個使用jackson的解析,它應該在tweet.json中逐行運行,並且每條推文(行)被解析爲POJO。因此,每一行是一個單一的POJO

如何做到這一點

這是我做了什麼

ObjectMapper mapper = new ObjectMapper(); 
ParsedTweets tweets = mapper.readValue(new File("tweet.json"), tweet.class); 

例如JSON文件

{"text": "MVA/Transport. Greater Hume (Hume Hwy, Holbrook, NSW 2644) at 7 Mar 2017 03:58 #NSWRFS #MVATransport", "user": {"id": "4721717942", "name": "NSW Fire Updates"}, "lang": "en", "coordinates": { "coordinates": [147.273696, -35.785469] , "type":"Point"}, "created_at": "Mon Mar 06 17:29:31 +0000 2017"} 
{"text": "Yes! Everything happens for a reason. The rain doesn't want the Swedru people to go& flood Westhills mall this evening", "user": {"id": "724288148", "name": "Pyper Pebbles"}, "lang": "en","created_at": "Mon Mar 06 17:19:49 +0000 2017"} 
{"text": "5 Sure-Fire Ways That Can Help You Achieve Better Success In Life , "user": {"id": "41049329", "name": "StarCentral Magazine"}, "lang": "en","created_at": "Mon Mar 06 17:08:36 +0000 2017"} 

tweet.class

public class Tweet { 
private String text; 
private String created_at; 
private User user; 
private Coordinates coordinates; 

public Tweet(){ 

} 

public String getText() 
{ 
    return text; 
} 

public void setText(String text) 
{ 
    this.text = text; 
} 

public String getCreated_at() 
{ 
    return created_at; 
} 

public void setCreated_at(String created_at) 
{ 
    this.created_at = created_at; 
} 

public User getUser() 
{ 
    return user; 
} 

public void setUser(User user) 
{ 
    this.user = user; 
} 

public Coordinates getCoordinates() 
{ 
    return coordinates; 
} 

public void setCoordinates(Coordinates coordinates) 
{ 
    this.coordinates = coordinates; 
} 

@Override 
public String toString() 
{ 
    return "ClassPojo [text = "+text+", created_at = "+created_at+", user = "+user+", coordinates = "+coordinates+"]"; 
} 

}

更新ReadWriteJson實施

public class ReadWriteJson { 



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

您可以加入json'的'一個例子以及Tweet'類的'結構 –

+0

當然,我只是說 – Kutam

回答

0

假設文件包含以下格式的鳴叫:

[{ 
    "id": "1", 
    "text" : "abc" 
}, { 
    "id": "2", 
    "text" : "def" 
}] 

而且tweet類有以下字段:

class tweet { 
    private String id; 
    private String text; 
    //getters and setters 
    public String toString(){ 
     return this.id + "," + this.name + "," + this.text + "," + this.coordinates + "," + this.created_at; 
} 

可以使用傑克遜TypeReference反序列化的列表,如:

ObjectMapper mapper = new ObjectMapper(); 
List<tweet> tweets = mapper.readValue(new File("tweet.json"), new TypeReference<List<tweet>>(){}); 

更新 按照更新後的類和文件結構,就需要逐行讀取文件中的行(因爲它不是一個有效的json)和反序列化入Tweet對象,如:

public class Test { 
    public static void main(String[] args) throws Exception { 
     List<Tweet> tweets = new ArrayList<>(); 
     ObjectMapper mapper = new ObjectMapper(); 
     try(BufferedReader reader = new BufferedReader(new FileReader("tweet.json"))){ 
      String line; 
      while((line = reader.readLine()) != null){ 
       tweets.add(mapper.readValue(line, Tweet.class)); 
      } 
     } 
    } 
} 
+0

嘿,對不起,我還以爲是交編輯但我的互聯網已經完成。請你再檢查一次。我剛剛發佈了它 – Kutam

+0

@Kutam更新了答案。 –

+0

是的,這就是我打算做的。逐行閱讀json文件...感謝您的洞察力。但它不工作有一個錯誤消息「錯誤:(28,6)java:解析時到達文件的結尾」。你能檢查我發佈的更新版本嗎 – Kutam