当前位置:   article > 正文

【FastJson】使用FastJson将Json数组转换成字符串_fastjson解析json文件,输出celldata转字符串

fastjson解析json文件,输出celldata转字符串

使用FastJson将Json数组转换成字符串


待处理的数据

在这里插入图片描述
代码实现
可以看出,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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174

然后我们将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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

结果
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/509635
推荐阅读
相关标签
  

闽ICP备14008679号