package json;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.io.*;
import java.util.ArrayList;
/**
* Created by xz86173 on 2/5/2016.
*/
public class JSONObject_v3 {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
JSONObject jsonObj_edgeid = null;
FileWriter fw = null;
FileReader fr_nd = null;
BufferedReader br_nd = null;
JSONObject json_nd = null;
FileWriter fw_nd = null;
try{
fr=new FileReader("C:\\Users\\");//获取文件流
br = new BufferedReader(fr); //将流整体读取。
fw = new FileWriter("C:\\Users\\");
fr_nd=new FileReader("C:\\Users\\");//获取文件流
br_nd = new BufferedReader(fr_nd); //将流整体读取。
// fw_nd = new FileWriter("C:\\Users\\");
String str;
JSONArray jsonArray_node = new JSONArray();
ArrayList edge_arr = new ArrayList();
JSONObject jsonObj_edge = new JSONObject();
while((str=br.readLine())!=null){//判断是否是最后一行
String[] s= str.split(",");
JSONObject jsonObj_attr_edge = new JSONObject();
jsonObj_attr_edge.put("weight", s[5]);
jsonObj_edgeid = new JSONObject();
jsonObj_edgeid.put("id", s[0]);
jsonObj_edgeid.put("label", s[4]);
jsonObj_edgeid.put("source", s[1]);
jsonObj_edgeid.put("target", s[2]);
jsonObj_edgeid.put("attributes",jsonObj_attr_edge);
edge_arr.add(jsonObj_edgeid);
}
String str_nd;
ArrayList nd_arr = new ArrayList();
JSONObject jsonObj_nd = new JSONObject();
while((str_nd=br_nd.readLine())!=null){//判断是否是最后一行
String[] s_nd= str_nd.split(",");
JSONObject jsonObj_attr_nd = new JSONObject();
// jsonObj_attr_nd.put("degree",s_nd[2]);
jsonObj_attr_nd.put("degree", s_nd[3]);
jsonObj_attr_nd.put("in_degree", s_nd[4]);
jsonObj_attr_nd.put("out_degree", s_nd[5]);
jsonObj_attr_nd.put("type_name", s_nd[6]);
jsonObj_nd = new JSONObject();
jsonObj_nd.put("id", s_nd[0]);
jsonObj_nd.put("label", s_nd[1]);
jsonObj_nd.put("attributes",jsonObj_attr_nd);
nd_arr.add(jsonObj_nd);
}
// fw_nd.write(nd_arr.toString());
jsonObj_edge.put("edges",edge_arr);
jsonObj_edge.put("nodes",nd_arr);
System.out.println(jsonObj_edge);
fw.write(jsonObj_edge.toString());
}catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
} catch (IOException e) {
System.out.println("读取文件失败");
}finally{
try {
fr.close();
br.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}