到目前为止,我有一个tweet.json文件,里面是tweet的集合。我构建了一个tweet.class,它是来自tweet中键值的对象的集合。
现在我想用jackson实现一个parses,它应该在tweet.json中逐行运行,并且每个tweet(行)都被解析为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 :(得分: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;
}
您可以使用Jackson的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));
}
}
}
}