JSONObject_v3

3 篇文章 0 订阅
3 篇文章 0 订阅
该博客展示了一个Java程序,用于读取CSV文件内容并转换为JSONObject。程序首先创建两个JSONObject,一个表示边(edges),一个表示节点(nodes)。然后,程序逐行读取文件,将每行数据拆分为字段,并将这些字段填充到对应的JSONObject中,最后将所有边和节点保存到JSONArray中,并写入到文件。
摘要由CSDN通过智能技术生成


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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值