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

我的代码如下,但是达不到效果,希望有高人指点一下
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个回答

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

楼主:
1:txt文件保存的数据格式是什么样式的呢?

楼主你的代码有点乱。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐