返回java中的GeoJSON对象

返回java中的GeoJSON对象,java,json,geojson,Java,Json,Geojson,我是GeoJSON新手,目前遇到了一个我不知道如何解决的问题 下面是我的FeatureCollection的一个片段 FeatureCollection fc = new FeatureCollection(); Map<String, Object> properties = new HashMap<String, Object>(); for(int i = 0; i < platforms.si

我是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();
据我所知,通过调试,正确的信息被放入功能中,但每当我返回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": {}

我不确定如何继续前进以使其反映我所需的输出。

您的问题是您正在调用
fc.toString(),它将命中默认的Object.toString()方法。这将转储一些类名+地址/id之类的字符串,具体取决于所使用的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)

谢谢,我能够得到您推荐的输出。但也有一个问题,格式与我希望Geojson输出的格式很难匹配。例如,输出中缺少第一行应为“type”:“FeatureCollection”。这是gson的格式问题吗?在原始帖子中添加了输出作为编辑。在您写“我希望我的输出看起来像这样,JSON代码的格式无效;
类型“:“FeatureCollection”
后缺少逗号。或括号。因此,如果“类型”和“功能”应该在同一级别/具有相同的父级,则1)可以编写另一个类来扩展FeatureCollection并添加该属性,2)编写另一个类,该类具有从FC复制的变量类型和功能,3)可以将该变量添加到当前FeatureCollection类,4)您可以编写自定义序列化程序(检查谷歌),5)您可以手动调整字符串
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;
    }
}