infinite_ksk 2017-05-17 07:05 采纳率: 0%
浏览 1006
已结题

从txt里读取数据生成类再将类转换成json

我的代码如下,但是达不到效果,希望有高人指点一下
package com.aminer;

import org.json.*;
import java.io.*;
import java.util.*;
/**

  • @author Administrator
    *
    */
    public class Paper {

    private int index;
    private List title;
    private List authors;
    private List affiliations;
    private int year;
    private List references;
    private List abstract_;
    private List venue;

public Paper(){
title = new ArrayList<>();
authors = new ArrayList<>();
affiliations = new ArrayList<>();
references = new ArrayList<>();
abstract_ = new ArrayList<>();
venue = new ArrayList<>();
}

public void setIndex(int index) {
this.index = index;
}

public void setYear(int year) {
this.year = year;
}

public void addAbstract(String abstrct) {
abstract_.add(abstrct);
}

public void addAffiliations(String affiliation) {
affiliations.add(affiliation);
}

public void addAuthor(String author ) {
authors.add(author);
}

public void addReference(String reference) {
references.add(reference);
}

public void addVenue(String venue_) {
venue.add(venue_);
}

public void addTitle(String title_) {
title.add(title_);
}

@Override
public String toString() {
JSONObject json = new JSONObject();
try{
json.put("index",index);
json.put("year",year);
for(String abstrct:abstract_){
json.append("abstract_",abstrct);
}
for(String affiliation:affiliations){
json.append("affiliations", affiliation);
}
for(String author:authors){
json.append("authors", author);
}
for(String reference:references){
json.append("references", reference);
}
for(String venue_:venue){
json.append("venue", venue_);
}
for(String title_:title){
json.append("title", title_);
}

}catch(JSONException e){
    e.printStackTrace();
}
return json.toString();

}

public void clear(){
index = 0;
year = 0;
title.clear();
authors.clear();
affiliations.clear();
references.clear();
abstract_.clear();
venue.clear();
}

}

package com.aminer;
import java.io.*;
import java.util.*;

import org.json.JSONObject;
public class Inf {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader (
new FileReader(new File("G:\test_txt\trty.txt")));
BufferedWriter writer = new BufferedWriter(
new FileWriter(new File("G:\test_txt\trty.json")));

    Paper paper = new Paper();
    String line;
    int count = 0;
    while((line =reader.readLine())!=null){
        if(line.length() <2)continue;
        char identifier = line.charAt(1);

        if(!(identifier=='i'||identifier=='*'||identifier=='@'||identifier=='o'||identifier=='t'||identifier=='c'||identifier=='%'||identifier=='!'))
        continue;
        if(identifier == 'i') {
            if(count>0) {
                writer.write(paper.toString()+"\n");
                paper.clear();
            }
            count++;
        }
        switch(identifier){
        case 'i' :
            paper.setIndex(Integer.valueOf(line.substring(7).trim()));
            break;
        case 't' :
            paper.setYear(Integer.valueOf(line.substring(10).trim()));
            break;
        case '@' :
            String authors = line.substring(3).trim();
            for (String author : authors.split(";")) {
                paper.addAuthor(author);
            }
            break;

        case 'o' :  
            paper.addAffiliations(String.valueOf(line.substring(3).trim()));
            break;
        case '*':
            paper.addVenue(String.valueOf(line.substring(3).trim()));
            break;
        case 'c' :
            paper.addVenue(String.valueOf(line.substring(3).trim()));
            break;

        case '!' :
            paper.addAbstract(String.valueOf(line.substring(3).trim()));
            break;
        case '%' :
            paper.addReference(String.valueOf(line.substring(3).trim()));
            break;
        default:
            System.out.println("not successful");

        }



    }
    reader.close();
    writer.close();
}
  • 写回答

2条回答

  • 御前提笔小书童 博客专家认证 2017-05-17 08:30
    关注

    取出数据存入map中然后直接转出json就行了,http://blog.csdn.net/qq_22260641/article/details/54605495

    评论

报告相同问题?

悬赏问题

  • ¥15 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?