package json;
import net.sf.json.JSONArray;@H_404_5@ import net.sf.json.JSONObject;
import java.io.*;@H_404_5@ import java.util.ArrayList;
/**@H_404_5@ * Created by xz86173 on 2/5/2016.@H_404_5@ */@H_404_5@ public class JSONObject_v3 {@H_404_5@ public static void main(String[] args) {@H_404_5@ FileReader fr = null;@H_404_5@ BufferedReader br = null;@H_404_5@ JSONObject jsonObj_edgeid = null;@H_404_5@ FileWriter fw = null;
FileReader fr_nd = null;@H_404_5@ BufferedReader br_nd = null;@H_404_5@ JSONObject json_nd = null;@H_404_5@ FileWriter fw_nd = null;
@H_404_5@ try{
fr=new FileReader("C:\\Users\\");//获取文件流@H_404_5@ br = new BufferedReader(fr); //将流整体读取。@H_404_5@ fw = new FileWriter("C:\\Users\\");
fr_nd=new FileReader("C:\\Users\\");//获取文件流@H_404_5@ br_nd = new BufferedReader(fr_nd); //将流整体读取。@H_404_5@ // fw_nd = new FileWriter("C:\\Users\\");
@H_404_5@
String str;@H_404_5@ JSONArray jsonArray_node = new JSONArray();@H_404_5@ ArrayList edge_arr = new ArrayList();@H_404_5@ JSONObject jsonObj_edge = new JSONObject();@H_404_5@ while((str=br.readLine())!=null){//判断是否是最后一行@H_404_5@ String[] s= str.split(",");
JSONObject jsonObj_attr_edge = new JSONObject();@H_404_5@ jsonObj_attr_edge.put("weight",s[5]);
jsonObj_edgeid = new JSONObject();@H_404_5@ jsonObj_edgeid.put("id",s[0]);@H_404_5@ jsonObj_edgeid.put("label",s[4]);@H_404_5@ jsonObj_edgeid.put("source",s[1]);@H_404_5@ jsonObj_edgeid.put("target",s[2]);@H_404_5@ jsonObj_edgeid.put("attributes",jsonObj_attr_edge);@H_404_5@ edge_arr.add(jsonObj_edgeid);@H_404_5@ }
@H_404_5@ String str_nd;@H_404_5@ ArrayList nd_arr = new ArrayList();@H_404_5@ JSONObject jsonObj_nd = new JSONObject();@H_404_5@ while((str_nd=br_nd.readLine())!=null){//判断是否是最后一行@H_404_5@ String[] s_nd= str_nd.split(",");
JSONObject jsonObj_attr_nd = new JSONObject();@H_404_5@ // jsonObj_attr_nd.put("degree",s_nd[2]);@H_404_5@ jsonObj_attr_nd.put("degree",s_nd[3]);@H_404_5@ jsonObj_attr_nd.put("in_degree",s_nd[4]);@H_404_5@ jsonObj_attr_nd.put("out_degree",s_nd[5]);@H_404_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(); } } } }