Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在使用Jackson将GeoJSON数据反序列化为对象后,如何访问该数据_Java_Json_Jackson_Geojson_Json Deserialization - Fatal编程技术网

Java 在使用Jackson将GeoJSON数据反序列化为对象后,如何访问该数据

Java 在使用Jackson将GeoJSON数据反序列化为对象后,如何访问该数据,java,json,jackson,geojson,json-deserialization,Java,Json,Jackson,Geojson,Json Deserialization,通过使用Jackson反序列化JSON文件,我创建了一个Java对象。我想在提供的地图模板中渲染数据,但我不确定如何访问它 下面是代码和JSON文件。我走对了吗?有人建议,如果我在main方法中使用功能类,我就可以访问该对象。类似于ex:featureobj=newfeature()然后使用obj.getType()访问任何变量。我想实际访问整个文件,使其呈现数据显示在世界风地图上 { "type": "FeatureCollection", "features": [

通过使用
Jackson
反序列化
JSON
文件,我创建了一个
Java
对象。我想在提供的地图模板中渲染数据,但我不确定如何访问它

下面是代码和
JSON
文件。我走对了吗?有人建议,如果我在main方法中使用
功能
类,我就可以访问该对象。类似于ex:
featureobj=newfeature()
然后使用
obj.getType()
访问任何变量。我想实际访问整个文件,使其呈现数据显示在
世界风地图上

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [102.0, 0.6]
            },
            "properties": {
                "prop0": "value0"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
                ]
            },
            "properties": {
                "prop1": 0.0,
                "prop0": "value0"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0],
                        [100.0, 0.0]
                    ]
                ]
            },
            "properties": {
                "prop1": {
                    "this": "that"
                },
                "prop0": "value0"
            }
        }
    ]
}
这是我的代码:

public class NetworkVisualizer extends ApplicationTemplate {

  public static class AppFrame extends ApplicationTemplate.AppFrame {

    public AppFrame() {
      super(true, true,
            false);

      // Size the World Window to take up the space typically used by the layer panel.
      Dimension size = new Dimension(1400, 800);
      this.setPreferredSize(size);
      this.pack();
      WWUtil.alignComponent(null, this, AVKey.CENTER);

      makeMenu(this);
    }


    protected static void makeMenu(final AppFrame appFrame) {
      final JFileChooser fileChooser = new JFileChooser();
      fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("JSON File", "json", "json"));

      JMenuBar menuBar = new JMenuBar();
      appFrame.setJMenuBar(menuBar);
      JMenu fileMenu = new JMenu("File");
      menuBar.add(fileMenu);

      JMenuItem openFileMenuItem = new JMenuItem(new AbstractAction("Open File...") {
        public void actionPerformed(ActionEvent actionEvent) {
          try {
            int status = fileChooser.showOpenDialog(appFrame);
            if (status == JFileChooser.APPROVE_OPTION) {

              //TODO Likely need to start here when handling parsing of GeoJSON!

              @JsonInclude(JsonInclude.Include.NON_NULL)
              @JsonPropertyOrder({
                      "type",
                      "geometry",
                      "properties"
              })
              class Feature {

                @JsonProperty("type")
                private String type;
                @JsonProperty("geometry")
                private Geometry geometry;
                @JsonProperty("properties")
                private Properties properties;
                @JsonIgnore
                private Map<String, Object> additionalProperties = new HashMap<String, Object>();

                @JsonProperty("type")
                public String getType() {
                  return type;
                }

                @JsonProperty("type")
                public void setType(String type) {
                  this.type = type;
                }

                @JsonProperty("geometry")
                public Geometry getGeometry() {
                  return geometry;
                }

                @JsonProperty("geometry")
                public void setGeometry(Geometry geometry) {
                  this.geometry = geometry;
                }

                @JsonProperty("properties")
                public Properties getProperties() {
                  return properties;
                }

                @JsonProperty("properties")
                public void setProperties(Properties properties) {
                  this.properties = properties;
                }

                @JsonAnyGetter
                public Map<String, Object> getAdditionalProperties() {
                  return this.additionalProperties;
                }

                @JsonAnySetter
                public void setAdditionalProperty(String name, Object value) {
                  this.additionalProperties.put(name, value);
                }
              }
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      });

      fileMenu.add(openFileMenuItem);


    }




  }
  public static void main(String[] args) {

    final AppFrame af = (AppFrame) start("World Wind JSON Network Viewer", AppFrame.class);
  }
}
jfc说的是创建局部变量 readValue正在抛出一个未处理的异常

,用于处理您可以使用的
JSON
有效负载,例如,版本中提供的库。要使用它,只需将其添加到
Maven
配置中:

<dependency>
    <groupId>de.grundid.opendatalab</groupId>
    <artifactId>geojson-jackson</artifactId>
    <version>1.12</version>
</dependency>
以上代码打印:

Feature{properties={prop0=value0}, geometry=Point{coordinates=LngLatAlt{longitude=102.0, latitude=0.6, altitude=NaN}} GeoJsonObject{}, id='null'}
Feature{properties={prop1=0.0, prop0=value0}, geometry=LineString{} MultiPoint{} Geometry{coordinates=[LngLatAlt{longitude=102.0, latitude=0.0, altitude=NaN}, LngLatAlt{longitude=103.0, latitude=1.0, altitude=NaN}, LngLatAlt{longitude=104.0, latitude=0.0, altitude=NaN}, LngLatAlt{longitude=105.0, latitude=1.0, altitude=NaN}]} GeoJsonObject{}, id='null'}
Feature{properties={prop1={this=that}, prop0=value0}, geometry=Polygon{} Geometry{coordinates=[[LngLatAlt{longitude=100.0, latitude=0.0, altitude=NaN}, LngLatAlt{longitude=101.0, latitude=0.0, altitude=NaN}, LngLatAlt{longitude=101.0, latitude=1.0, altitude=NaN}, LngLatAlt{longitude=100.0, latitude=1.0, altitude=NaN}, LngLatAlt{longitude=100.0, latitude=0.0, altitude=NaN}]]} GeoJsonObject{}, id='null'}
如您所见,您可以以非常结构化的方式访问每个属性。您不需要创建自己的
POJO
,只需使用已经实现和测试的
POJO
模型即可。在您的示例中,您可以这样做:

if (status == JFileChooser.APPROVE_OPTION) {
    File selectedFile = jfc.getSelectedFile();
    ObjectMapper objectMapper = new ObjectMapper();
    FeatureCollection features = objectMapper.readValue(selectedFile, FeatureCollection.class);
}
另见:


您真的在if语句中定义了类吗?那不可能是合法的…?我想是。。。但不要那样做。。。。这使得代码很难看,很难阅读。谢谢你的帮助。你建议的两种方法我都试过了。双方都不想合作。第一个使用GeoJSON的方法不起作用,它没有按照建议打印出行。使用POJO的第二种方法不希望合作。我将抛出我在帖子中遇到的错误。@brianmsantos,我发现您在
Java
方面有一些基本问题。请先尝试阅读一些教程:。之后,你应该更容易发现问题。您混淆了许多概念,变量被误用,方法声明错误。此外,您不应该在应用程序中声明第二个
main
方法。你需要学习如何传递参数。
Feature{properties={prop0=value0}, geometry=Point{coordinates=LngLatAlt{longitude=102.0, latitude=0.6, altitude=NaN}} GeoJsonObject{}, id='null'}
Feature{properties={prop1=0.0, prop0=value0}, geometry=LineString{} MultiPoint{} Geometry{coordinates=[LngLatAlt{longitude=102.0, latitude=0.0, altitude=NaN}, LngLatAlt{longitude=103.0, latitude=1.0, altitude=NaN}, LngLatAlt{longitude=104.0, latitude=0.0, altitude=NaN}, LngLatAlt{longitude=105.0, latitude=1.0, altitude=NaN}]} GeoJsonObject{}, id='null'}
Feature{properties={prop1={this=that}, prop0=value0}, geometry=Polygon{} Geometry{coordinates=[[LngLatAlt{longitude=100.0, latitude=0.0, altitude=NaN}, LngLatAlt{longitude=101.0, latitude=0.0, altitude=NaN}, LngLatAlt{longitude=101.0, latitude=1.0, altitude=NaN}, LngLatAlt{longitude=100.0, latitude=1.0, altitude=NaN}, LngLatAlt{longitude=100.0, latitude=0.0, altitude=NaN}]]} GeoJsonObject{}, id='null'}
if (status == JFileChooser.APPROVE_OPTION) {
    File selectedFile = jfc.getSelectedFile();
    ObjectMapper objectMapper = new ObjectMapper();
    FeatureCollection features = objectMapper.readValue(selectedFile, FeatureCollection.class);
}