java构造和生成固定的json格式(geojson为例)

2023-05-16

java构造和生成json格式(geojson为例)

一、所要构造的json格式

在这里插入图片描述

二、思路和步骤

1.题外说明

本文是先解析读入的txt文件,然后建立对应的java类来接受解析的某些值,用了自己封装的TxtToPolygon(jsonpath, shpPath)方法。因此大家不必在意本代码中所用到的Attributes、Feature类(实际是json中本人打算put的数据),只需知道什么样的json格式该怎么“搭架子”和填值即可。
我需要填入的值中有coordinates坐标,因此需要提前准备:

//txt文件路径
        String jsonpath="E:\\河南省乡镇点\\12.txt";
        //shp文件所在的位置
        String shpPath = "E:/河南省乡镇点/GIS/";
        makeDir(shpPath);
        //获取coordinates中的点
        List<Point> pointsList = TxtToPolygon(jsonpath, shpPath);

具体思路及代码如下(示例):
1.采用“从外到内,层层递进剥皮”的思想分析,首先看上面geojson的格式,最外面是一层“{}”,因此需要定义一个最外层的JSON对象,方便往里面放子JSON对象或者JSON数组。
2.再看里面第二层,其含有的JSON对象有:crs(用{}包裹的),另外还有一个JSON数组:features(用[]包裹的)。type和name的键值对既不属于JSON对象也不属于JSON数组,直接put填入最外层的JSIN对象。因此还需要定义一个crs的JSON对象和一个features的JSON数组。
3.再看crs的下一层即第三层(也就是crs里面的),crs对象中又嵌套了一个对象properties,此对象中包含一个键值对,至此,crs递进到底了。因此又需要定义一个properties的JSON对象。
4.再看features的下一层,features数组中又包含两个JSON对象,第一个JSON对象中又包含一个对象properties。第二个JSON对象geometry中又包含了一个coordinates的JSON数组(用[]包裹)。
5.综上所述,需要多少个JSON对象及数组就一目了然了,然后往对应的JSON对象或数组中put对应层级的键值对或对象、数组即可,采用“从内到外,层层概化”的思想填值即可。

2.实现代码

需要构建的JSON对象或数组(装数据的”袋子“)代码如下:

//构造geojson格式
        //最外层JSONObject
        JSONObject object = new JSONObject();
        //crs的JSONArray
        JSONObject crsObject = new JSONObject();
        //properties1的JSONArray
        JSONObject properties1Object = new JSONObject();
        //features的JSONArray
        JSONArray featuresArray = new JSONArray();
        //features中第一个对象featuresObj1
        JSONObject featuresObj1 = new JSONObject();
        //features中第一个对象featuresObj1中的properties2的JSONObject
        JSONObject properties2Object = new JSONObject();
        //features中geometry对象
        JSONObject geometryObj = new JSONObject();
        //features中geometry对象中coordinatesArray
        JSONArray coordinatesArray= new JSONArray();

2.1 crs对象的处理

crs中properties对象的处理

  properties1Object.put("name", "urn:ogc:def:crs:OGC:1.3:CRS84");

向crs中填数据

  crsObject.put("type","name");
  crsObject.put("properties",properties1Object);

2.2 features对象的处理

2.2.1 features对象中第一个对象中的properties对象的处理(注意有3层)
//features中的对象
  test.entity.Feature feature=new test.entity.Feature();
  feature = ReadTxtFile(jsonpath);
    //attribute属性表
  Attributes attributes=feature.getAttributes();
  //向features对象中第一个对象中的properties对象中填数据
  properties2Object.put("userId",attributes.getUserId());
  properties2Object.put("farmCode",attributes.getFarmCode());
  properties2Object.put("name",attributes.getName());
  properties2Object.put("radius",attributes.getRadius());
  properties2Object.put("job",attributes.getJob());
  properties2Object.put("content",attributes.getContent());

最后向features对象中第一个对象中填数据

  featuresObj1.put("id",0);
  featuresObj1.put("type","Feature");
  featuresObj1.put("properties",properties2Object);
2.2.2 features对象中第二个对象中的coordinates数组的处理
//coordinates数组中嵌套一个 List<List<T>>的数组格式
//先获取List<List<T>>这个数组
 List<List<Double>> coordinates=new ArrayList<>();
        List<Double> coordtemp=new ArrayList<>();
        for (int i = 0; i < pointsList.size(); i++) {
            coordtemp.add(pointsList.get(i).getX());
            coordtemp.add(pointsList.get(i).getY());
            coordinates.add(coordtemp);
            coordtemp.clear();
        }
        coordtemp.add(pointsList.get(0).getX());
        coordtemp.add(pointsList.get(0).getY());
        coordinates.add(coordtemp);
 //将上面数组推入coordinatesArray
 coordinatesArray.put(coordinates);

向features对象中第二个对象中填数据

  geometryObj.put("coordinates",coordinatesArray);
  geometryObj.put("type","Polygon");

最后将第三层两个对象填入features对象

  featuresArray.put(featuresObj1);
  featuresArray.put(geometryObj);

2.3 最外层对象(整个geojson对象)的处理

  object.put("features",featuresArray);
  object.put("crs",crsObject);
  object.put("name",attributes.getName()+"_polygon");
  object.put("type","FeatureCollection");

完整代码如下(不可运行,供代码方便复制)

  public static void main(String[] args) throws IOException {
        //txt文件路径
        String jsonpath="E:\\河南省乡镇点\\12.txt";
        //shp文件所在的位置
        String shpPath = "E:/河南省乡镇点/GIS/";
        makeDir(shpPath);
        //获取coordinates中的点
        List<Point> pointsList = TxtToPolygon(jsonpath, shpPath);

        //构造geojson格式
        //最外层JSONObject
        JSONObject object = new JSONObject();
        //crs的JSONArray
        JSONObject crsObject = new JSONObject();
        //properties1的JSONArray
        JSONObject properties1Object = new JSONObject();
        //features的JSONArray
        JSONArray featuresArray = new JSONArray();
        //features中第一个对象featuresObj1
        JSONObject featuresObj1 = new JSONObject();
        //features中第一个对象featuresObj1中的properties2的JSONObject
        JSONObject properties2Object = new JSONObject();
        //features中geometry对象
        JSONObject geometryObj = new JSONObject();
        //features中geometry对象中coordinatesArray
        JSONArray coordinatesArray= new JSONArray();

        //crs中的对象
        properties1Object.put("name", "urn:ogc:def:crs:OGC:1.3:CRS84");
        crsObject.put("type","name");
        crsObject.put("properties",properties1Object);

        //features中的对象
        test.entity.Feature feature=new test.entity.Feature();
        feature = ReadTxtFile(jsonpath);
          //attribute属性表
        Attributes attributes=feature.getAttributes();
        properties2Object.put("userId",attributes.getUserId());
        properties2Object.put("farmCode",attributes.getFarmCode());
        properties2Object.put("name",attributes.getName());
        properties2Object.put("radius",attributes.getRadius());
        properties2Object.put("job",attributes.getJob());
        properties2Object.put("content",attributes.getContent());

        featuresObj1.put("id",0);
        featuresObj1.put("type","Feature");
        featuresObj1.put("properties",properties2Object);

        List<List<Double>> coordinates=new ArrayList<>();
        List<Double> coordtemp=new ArrayList<>();
        for (int i = 0; i < pointsList.size(); i++) {
            coordtemp.add(pointsList.get(i).getX());
            coordtemp.add(pointsList.get(i).getY());
            coordinates.add(coordtemp);
            coordtemp.clear();
        }
        coordtemp.add(pointsList.get(0).getX());
        coordtemp.add(pointsList.get(0).getY());
        coordinates.add(coordtemp);
        System.out.println(coordinates);
        coordinatesArray.put(coordinates);
        geometryObj.put("coordinates",coordinatesArray);
        geometryObj.put("type","Polygon");
        featuresArray.put(featuresObj1);
        featuresArray.put(geometryObj);
        
        object.put("features",featuresArray);
        object.put("crs",crsObject);
        object.put("name",attributes.getName()+"_polygon");
        object.put("type","FeatureCollection");
        System.out.println(object.toString());
    }

总结

用{}包裹的属于JSON对象,用[]包裹属于JSON数组,对应好层级put相应的值即可。
对象中put”键值对“,其中的”值“可以是基本类型的值,也可以是一个JSON对象或数组(嵌套情况下)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java构造和生成固定的json格式(geojson为例) 的相关文章

  • 域名已经停止访问

    最近自己的产品链接被微信封了 xff0c 因为推广里面涉及一些引诱的内容 xff0c 但公司又不得采用这样方案 xff0c 所以公司领导需要有工具能够试试检测是否域名被封 xff0c 一旦被封就切换域名 xff0c 这样子就会大大减少损失
  • 基于springboot书籍推荐系统设计与实现的源码+文档

    DROP TABLE IF EXISTS 96 book classification 96 CREATE TABLE 96 book classification 96 96 book classification id 96 int 1
  • 超级产品:喜茶,凭什么能估值90亿

    疫情期间 xff0c 呆在家里的这些人 xff0c 最怀念的莫过于以前那一段靠奶茶续命的的日子了 肺炎快点结束吧 xff01 我想出门晒太阳 xff0c 吹海风 xff0c 我想念喜茶了 喜茶居然成为这些人的一个生活场景符号了 喜茶是一家什
  • powershell 识别插入U盘盘符

    Get Volume Where Object DriveType eq 34 Removable 34
  • 解决Xrdp远程登录Ubuntu需要多次输入密码认证的问题

    问题描述 使用 xrdp 远程登录Ubuntu xff0c 总是会出现弹框要求输入密码 描述信息一般是 xff1a 需要授权来创建色彩管理设备 Authentication is required to create a color man
  • BottomNavigationView与Navigation架构组件结合使用时,setupWithNavController不生效问题解决

    最近学习jetpack Navigation导航组件时 xff0c 用BottomNavigationView与Navigation配合完成主页单activity与多fragment导航功能 xff0c 一切准备就绪 xff0c 却发现通过
  • vue项目报错Expected indentation of 2 spaces but found 4

    报错原因 严格的检查缩进问题 xff0c 不是报错 xff0c 我们可以关闭这个检查规则 解决方案 找到以下的文件 xff0c 添加 34 indent 34 34 off 34 2 然后在终端输入npm run dev 如果觉得能帮助到你
  • Shared-memory Based Ring Buffer

    This post explains how to implement a ring buffer that can be shared between multiple processes For the simplicity and e
  • Ubuntu系统无法进入图形化界面【笔记】

    服务器位置 显示器变更后 xff0c Ubuntu系统无法进入图形化界面 现场情况 xff1a alt 43 ctrl 43 F1 无反应 xff0c alt 43 ctrl 43 F1 F6 有反应 1 alt 43 ctrl 43 F2
  • 生产者与消费者设计模式(一对一,一对多,多对多)——Java实现

    第一次发文 xff0c 如有错误 xff0c 欢迎指正 1 首先 xff0c 我们创建一个消费对象 public class StaticVariate public static List Pc 61 new ArrayList 2 创建
  • Linux 下 FTP 连接使用方式及常用配置

    Linux 下 FTP 连接使用方式及常用配置 FTP 协议介绍传输方式支持模式FTP 客户端注意事项FTP 协议连接步骤FTP 常用命令 Linux下 FTP 常用命令Linux 下连接 FTP 服务器Linux下 FTP 目录操作Lin
  • IDEA开发工具使用 git 创建项目、拉取分支、合并分支

    工作中多人使用版本控制软件协作开发 xff0c 常见的应用场景归纳如下 xff1a 假设小组中有两个人 xff0c 组长小张 xff0c 组员小袁 场景一 xff1a 小张创建项目并提交到远程Git仓库 场景二 xff1a 小袁从远程Git
  • CSS应用

    任务4 1 使用CSS样式美化购物列表页面中的菜单导航栏 任务4 2 使用CSS样式美化购物列表页面中的商品展示区 任务5 1 使用CSS样式实现购物列表页面的整体布局 任务5 2 使用CSS样式实现购物列表页面的左侧导航栏部分
  • XPath的使用(基本的使用方式,心得,持续添加)

    1 心得 xff1a 解析网页时 xff0c 使用过HtmlAgilityPack这个类库 xff0c 里面用到了XPath来查找结点 在使用过程中 xff0c 因为html文本的标签十分多 xff0c 大部分元素是没有id属性的 xff0
  • 姿态解算原理(一)——旋转矩阵

    像我们常见的MPU6050 MPU9250等等都是一种捷联式的惯性元件 xff0c 还是一种低成本的 xff0c 还有一种是平台式的惯性导航 xff0c 不过我们能够用得起的就是低成本的MEMS惯性元件 xff0c 本文的内容是姿态解算的原
  • linux 设置自动关机和重启命令shutdown

    1 shutdown使用命令 xff1a Shutdown 选项 时间 参数解释 xff1a k 不真关机向所有用户提示警告信息 r 关机后立即重新启动 h 关机 f 快速关机重启动时跳过fsck n 快速关机不经过init 程序 c 取消
  • manifest.json

    34 name 34 34 Shopro开源商城 34 34 appid 34 34 UNI 34 34 description 34 34 Shopro开源商城 34 34 versionName 34 34 1 3 2 34 34 ve
  • 普通人对AI的看法

    就发展前景来看 xff0c 人工智能无疑将是现阶段与今后很长时间内的全球性热点 这是一个可以预见性的历史潮流 xff0c 无可阻挡 xff0c 一旦它出现一定会对现代互联网的结构会产 生颠覆性的改变 它将重新定义现代互联网的理念 xff0c
  • expect_out(buffer)中包含send的数据

    expect out buffer 中包含send的数据 我一直以为在Expect中一旦执行send之后 xff0c expect out buffer 就会被清空 xff0c 直到有新的数据被填入 xff0c 而恰恰就是这些数据被用在ex
  • Python在Linux环境中获取文件名并读取文件内容格式化输出

    Python在Linux环境中获取文件名并读取文件内容格式化输出 代码示例 xff1a span class token comment usr bin python3 span span class token comment codin

随机推荐