因为工作需要需要读取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);
}
}
}