在Java中返回GeoJSON对象

HangoutGuy

我是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": {}

我不确定该怎么做才能使它反映我想要的输出。

杰伊C667

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

与其调用toString(),不如使用google gson之类的JSON库,并在代码底部添加几行:

    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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章