宇筱博客

  • 解决办法
  • 学无止境
  • 记录时光
  • 百宝箱
宇筱博客
点滴记忆,汇聚成海。
  1. 首页
  2. 解决办法
  3. 正文

Java读取json文件并对json数据进行读取、添加、删除与修改操作

2016年1月1日 1465点热度 0人点赞 0条评论

1.介绍

开发过程中经常会遇到json数据的处理,而单独对json数据进行增删改并不方便,尤其是Geojson文件的处理,通过对网络资料的整理总结,下面介绍Java语言方法对json数据进行读取、添加、删除与修改操作。

2.说明

Java语言操作json对象,需引入json数据操作库(org.json.jar)文件,可通过网络搜索寻找,另外本文附件代码中已包含,在Eclipse或其它编译工具中直接引入即可。

本文通过一个Geojson文件(HK_geo.json)为例进行介绍,nameID.txt为新json新添加字段(key)所对应的值(value)。HK_new.json为修改后的新文件。

3.json样例

{
   "features" : [
      {
         "geometry" : {
            "coordinates" : [
               [
                  [ 114.0845110, 22.5199910 ],
                  [ 114.0756680, 22.5174660 ],
                  [ 114.0781940, 22.5162030 ],
                  [ 114.079460, 22.5166230 ],
                  [ 114.0828250, 22.519150 ],
                  [ 114.0845110, 22.5199910 ]
               ]
            ],
            "type" : "Polygon"
         },
         "properties" : {
            "ID_0" : 102,
            "ID_1" : 18,
            "ISO" : "HKG",
            "name" : "Yuen Long"
         },
         "type" : "Feature"
      }
   ],
   "type" : "FeatureCollection"
}

4.代码

package json;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;

public class JsonConvert {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // 读取nameID.txt文件中的NAMEID字段(key)对应值(value)并存储
        ArrayList<String> list = new ArrayList<String>();
        BufferedReader brname;
        try {
            brname = new BufferedReader(new FileReader("src/json/nameID.txt"));// 读取NAMEID对应值
            String sname = null;
            while ((sname = brname.readLine()) != null) {
                // System.out.println(sname);
                list.add(sname);// 将对应value添加到链表存储
            }
            brname.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // 读取原始json文件并进行操作和输出
        try {
            BufferedReader br = new BufferedReader(new FileReader(
                    "src/json/HK_geo.json"));// 读取原始json文件
            BufferedWriter bw = new BufferedWriter(new FileWriter(
                    "src/json/HK_new.json"));// 输出新的json文件
            String s = null, ws = null;
            while ((s = br.readLine()) != null) {
                // System.out.println(s);
                try {
                    JSONObject dataJson = new JSONObject(s);// 创建一个包含原始json串的json对象
                    JSONArray features = dataJson.getJSONArray("features");// 找到features的json数组
                    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字段
                    }
                    ws = dataJson.toString();
                    System.out.println(ws);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            bw.write(ws);
            // bw.newLine();

            bw.flush();
            br.close();
            bw.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

5.源代码下载

http://download.csdn.net/detail/qing_yun/8895797

原文链接:Java读取json文件并对json数据进行读取、添加、删除与修改操作_Qing_yun的博客-CSDN博客_java json添加数据

标签: 暂无
最后更新:2022年3月24日

小渔民

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2025 宇筱博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备15017825号-2