通过Gson读取大文本json文件案例

因为工作需要需要读取4GB的json文件,所以采用了Gson,通过bean的方式获取到数据

	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
	</dependency>

	<dependency>
		<groupId>com.google.code.gson</groupId>
		<artifactId>gson</artifactId>
		<version>2.2.4</version>
	</dependency>

json片段

{
  "content": [
    {
      "field": "government",
      "en": {
        "source": "unknown",
        "sentence": "Maori language schools have increased participation rates, but alone cannot counterbalance the other societal problems that cause students to drop out of school."
      },
      "ko": {
        "source": "unknown",
        "sentence": "마오리어 학교의 입학률이 높아지고 있지만, 이것만으로는 학생들의 중퇴를 초래하는 다른 사회적 문제들을 해소할 수 없다."
      },
      "source": "unknown",
      "sentence": "毛利语学校的入学率提高,但单靠这一点不能消除造成学生辍学的其他社会问题。"
    },
    {
      "field": "science",
      "en": {
        "source": "US20160011231A1",
        "sentence": "However, when it is necessary to control the movement amount in the Z direction as in the force curve measurement, the closed loop control based on the detection signal of the non-contact sensor 130c in the Z direction may be performed."
      },
      "ko": {
        "source": "unknown",
        "sentence": "그러나 힘곡선 측정과 같이 Z방향의 이동량을 제어해야 할 때에는 비접촉형 센서 130c의 Z방향의 검출신호를 기반으로 하는 폐루프 제어를 할 수 있다."
      },
      "source": "CN105320152A",
      "sentence": "但是,在如力曲线测定那样需要控制Z方向的移动量时,可进行基于非接触型传感器130c的Z方向的检测信号的闭环控制。"
    }
  ]
}

根据json格式创建对象接收

public class Language {
    private String source;
    private String sentence;

    // Getters and setters are not required for this example.
    // GSON sets the fields directly using reflection.

    @Override
    public String toString() {
        return source + " - " + sentence;
    }
}
public class Content {
    private String field;
    private String source;
    private String sentence;
    private Language en;
    private Language ko;

    public Language getEn() {
        return en;
    }
    public void setEn(Language en) {
        this.en = en;
    }
    public Language getKo() {
        return ko;
    }
    public void setKo(Language ko) {
        this.ko = ko;
    }
    @Override
    public String toString() {
        return field + " - " + en+" - "+ko+" - "+source+" - "+sentence;
    }
}
public class Result {
    private List<Content> contentList;

    public List<Content> getContentList() {
        return contentList;
    }

    public void setContent(List<Content> contentList) {
        this.contentList= contentList;
    }

    @Override
    public String toString() {
        String str = "";
        for(Content content1:contentList){
            str+=content1;
        }
        return str;
    }
}

通过Gson读取大文本json文件

// 
public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("F:/zh_en_ko.json");
        try(Reader reader = new InputStreamReader(fis,"UTF-8")){
            Gson gson = new GsonBuilder().create();
            Result p = gson.fromJson(reader, Result.class);
            List<Content> content = p.getContentList();
            Integer count = 0;
            for (Content content1:content) {
                System.out.println(content1.toString());
                count++;
                System.out.println(count);
            }
        }
    }
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值