Java读写Json文件

java读写JSon文件有以下:

(1)将JSon文件通过普通文本文件的方式进行读取,并且存放在一个字符串中。

(2)创建一个JSon对象,并将字符串初始化到JSon对象中,这里需要引入一个JSon的包Json-org.jar引入包的具体做法如下:

  • 下载json-org.jar的包放在本地目录中
  • 读写json文件所在的项目中buildpath--》configureBuidpath--》add external jar然后将json-org.jar包导入到项目中

(3)通过创建的JSon对象完成对数据的操作和处理

(4)将操作完成的JSon对象转换成字符串

(5)将字符串存放在指定的Json文件中

下面的代码操作的是src/json目录下的test.json文件,实现的功能是添加“nameID”字段并且删除“ISO”字段,并存放在src/json目录下newFile.json新的文件中,文件的内容如下:

{
"type": "FeatureCollection",  
    "features": [{  
        "type": "Feature",  
        "properties": {  
            "name": "Yuen Long",  
            "ID_0": 102,  
            "ID_1": 18,  
            "ISO": "HKG"  
        },  
        "geometry": {  
            "type": "Polygon",  
            "coordinates": [[[114.084511, 22.519991], [114.075668, 22.517466], [114.078194, 22.516203], [114.079460, 22.516623], [114.082825, 22.519150], [114.084511, 22.519991]]]  
        }  
    }]
}

代码如下:

public class test {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>(); 
        list.add("admin");
        //读取json文件
        try {
            BufferedReader br = new BufferedReader(new FileReader("src/json/test.json"));
            BufferedWriter bw=new BufferedWriter(new FileWriter("src/json/newFile.json"));
            String str=null;
            String data="";
            while((str=br.readLine())!=null) {
                data=data+str+"\n";
            }
            JSONObject dataJson = new JSONObject(data);
            JSONArray features=dataJson.getJSONArray("features");
            for (int i = 0; i < features.length(); i++) {  
                JSONObject info = features.getJSONObject(i);// 获取features数组的第i个json对象  
                JSONObject properties = info.getJSONObject("properties");// 找到properties的json对象  
                String name = properties.getString("name");// 读取properties对象里的name字段值  
                System.out.println(name);  
                properties.put("NAMEID", list.get(i));// 添加NAMEID字段  
                // properties.append("name", list.get(i));  
                System.out.println(properties.getString("NAMEID"));  
                properties.remove("ISO");// 删除ISO字段 
                String ws=dataJson.toString();
                System.out.println(ws);
                bw.write(ws);  
                // bw.newLine();  
                bw.flush();  
                br.close();  
                bw.close();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// 读取原始json文件 
 catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}    
    }

}

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值