赞
踩
代码实现
可以看出,Json数据像一个数组,里面的每一个元素都是一个对象,所以我们首先要定义出一个对象来存储数据
public class Film { private String rating; private String rank; private String cover_url; private String is_playable; private String id; private String types; private String regions; private String title; private String url; private String release_date; private String actor_count; private String vote_count; private String score; private String actors; private String is_watched; @Override public String toString() { return rating + "\t" + rank + "\t" + cover_url + "\t" + is_playable + "\t" + id + "\t" + types + "\t" + regions + "\t" + title + "\t" + url + "\t" + release_date + "\t" + actor_count + "\t" + vote_count + "\t" + score + "\t" + actors + "\t" + is_watched; } public void set(String rating, String rank, String cover_url, String is_playable, String id, String types, String regions, String title, String url, String release_date, String actor_count, String vote_count, String score, String actors, String is_watched) { this.rating = rating; this.rank = rank; this.cover_url = cover_url; this.is_playable = is_playable; this.id = id; this.types = types; this.regions = regions; this.title = title; this.url = url; this.release_date = release_date; this.actor_count = actor_count; this.vote_count = vote_count; this.score = score; this.actors = actors; this.is_watched = is_watched; } public String getRating() { return rating; } public void setRating(String rating) { this.rating = rating; } public String getRank() { return rank; } public void setRank(String rank) { this.rank = rank; } public String getCover_url() { return cover_url; } public void setCover_url(String cover_url) { this.cover_url = cover_url; } public String getIs_playable() { return is_playable; } public void setIs_playable(String is_playable) { this.is_playable = is_playable; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTypes() { return types; } public void setTypes(String types) { this.types = types; } public String getRegions() { return regions; } public void setRegions(String regions) { this.regions = regions; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getRelease_date() { return release_date; } public void setRelease_date(String release_date) { this.release_date = release_date; } public String getActor_count() { return actor_count; } public void setActor_count(String actor_count) { this.actor_count = actor_count; } public String getVote_count() { return vote_count; } public void setVote_count(String vote_count) { this.vote_count = vote_count; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public String getActors() { return actors; } public void setActors(String actors) { this.actors = actors; } public String getIs_watched() { return is_watched; } public void setIs_watched(String is_watched) { this.is_watched = is_watched; } }
然后我们将jJson数据进行转换
import com.alibaba.fastjson.JSONReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; public class Json_txt { public static void main(String[] args) throws Exception { //因为是Json数组,因此使用JsonReader读取 JSONReader reader = new JSONReader(new FileReader("D:\\MP\\豆瓣\\data.json")); BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\MP\\豆瓣\\data.txt")); //数组开始读取 reader.startArray(); while (reader.hasNext()) { //将Json数组的每一个元素转换成对象 Film film = reader.readObject(Film.class); //写出对象 bw.write(film.toString()); bw.write("\n"); //刷新,防止数据残留 bw.flush(); } //结束数组读取 reader.endArray(); bw.close(); } }
结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。