【问题标题】:Returning a GeoJSON object in java在 java 中返回 GeoJSON 对象
【发布时间】:2021-02-02 21:57:54
【问题描述】:

我是 GeoJSON 的新手,我目前遇到了一个我不知道该怎么做的问题。

这是我的 FeatureCollection 的样子

        FeatureCollection fc = new FeatureCollection();
        Map<String, Object> properties = new HashMap<String, Object>();
        
        for(int i = 0; i < platforms.size(); i ++) {
            Feature feature = new Feature();
            Point geometry = new Point();
            Dto dto = platforms.get(i);
            Position position = new Position(dto.getLat(), dto.getLon());

            fc.addFeature(feature);
            geometry.setCoordinates(position);
            feature.setGeometry(geometry);
            feature.setProperties(properties);

            properties.put("name", dto.getName());
            properties.put("msl", dto.getMsl());
            properties.put("id", dto.getId());
 
        }
        return fc.toString();

我希望我的输出如下所示:

{
                "type":"FeatureCollection"
                features":[ 
                {
                            "type":"Feature"
                            "geometry": {
                                "type":"Point"
                                "coordinates":[
                                   -120.200000,
                                     10.100000 
                                     ] 
                                  }, 
                                  "properties": {
                                     "name": "1"
                                     "height": "100.00"
                                     "id": "null"
                                  }
                               }
                {
                            "type":"Feature"
                            "geometry": {
                                "type":"Point"
                                "coordinates\":[
                                  -130.200000,
                                     20.100000
                                    ] \n "
                                  }, \n "
                                  "properties": { "
                                     "name": "2"
                                     "height": "100.00"
                                     "id": "null"
                                  } 
                              } 
                            ] 
                 }  

据我所知,通过调试,正确的信息被放入功能中,但每当我返回 featureCollection 时,我都会得到:

mil.nga.sf.geojson.FeatureCollection@366ac49b

我对 geojson 了解不多,但似乎我错误地返回了 FeatureCollection 并打印出它的名称或其他内容。

简单来说,如何打印 FeatureCollection 的内容?

编辑: 这是我实现gson后得到的输出。

{
  "features": [
    {
      "feature": {
        "geometry": {
          "x": 10.1,
          "y": -120.2,
          "z": null,
          "m": null,
          "geometryType": "POINT",
          "hasZ": false,
          "hasM": false
        },
        "properties": {
          "msl": 100.0,
          "name": "1",
          "id": null
        }
      },
      "id": null,
      "bbox": null,
      "foreignMembers": {}
    },
    {
      "feature": {
        "geometry": {
          "x": 20.1,
          "y": -130.2,
          "z": null,
          "m": null,
          "geometryType": "POINT",
          "hasZ": false,
          "hasM": false
        },
        "properties": {
          "msl": 100.0,
          "name": "2",
          "id": null
        }
      },
      "id": null,
      "bbox": null,
      "foreignMembers": {}
    }
  ],
  "bbox": null,
  "foreignMembers": {}

我不确定下一步该怎么做才能反映我想要的输出。

【问题讨论】:

    标签: java json geojson


    【解决方案1】:

    您的问题是您正在调用fc.toString();,这将触发默认的 Object.toString() 方法。这将转储一些类名+地址/id 类字符串,具体取决于所使用的 JVM。

    您应该使用像 google gson 这样的 JSON 库,而不是调用 toString(),并添加 代码底部的几行:

        final GsonBuilder gb = new GsonBuilder();
        gb.setPrettyPrinting();
        gb.serializeNulls();
        // gb.excludeFieldsWithoutExposeAnnotation(); // maybe use for more control
    
        final Gson gson = gb.create();
        final String jsonText = gson.toJson(fc);
        return jsonText;
    

    还可以考虑编写一个实用程序类,使用您选择的默认设置为您执行此操作:

    public class MyJsonUtil {
        static public String toJSON(final Object pObject) {
            final GsonBuilder gb = new GsonBuilder();
            gb.setPrettyPrinting();
            gb.serializeNulls();
            // gb.excludeFieldsWithoutExposeAnnotation(); // maybe use for more control
    
            final Gson gson = gb.create();
            final String jsonText = gson.toJson(pObject);
            return jsonText;
        }
    }
    

    然后在您的代码末尾您只需调用return MyJsonUtil.toJSON(fc)

    【讨论】:

    • 谢谢你,我能够得到你的推荐的输出。但是有一个问题,格式与我希望我的 Geojson 输出看起来的格式几乎不匹配。例如,输出中缺少应该是 "type":"FeatureCollection" 的第一行。这是用 gson 格式化的东西吗?
    • 将原始帖子中的输出添加为编辑。
    • 您在哪里写“我希望我的输出看起来像这样,JSON 代码的格式无效; "type":"FeatureCollection" 后面少了一个逗号。或括号。因此,如果“类型”和“功能”应该在同一级别/具有相同的父级,您可以 1)编写另一个扩展您的 FeatureCollection 并添加一个属性的类,2)编写另一个具有变量类型的类以及从 FC 复制的功能,3)您可以将该变量添加到您当前的 FeatureCollection 类中,以及 4)您可以编写自定义序列化程序(检查 google)和 5)您可以手动调整字符串
    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 2017-07-31
    • 2017-02-24
    • 2012-04-23
    • 2011-01-24
    • 2020-02-17
    • 2014-05-27
    • 2020-11-20
    相关资源
    最近更新 更多