微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JSONObject_v3



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(); } } } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐