我通过使用Java
反序列化JSON
文件来创建Jackson
对象。我想在提供的地图模板中呈现数据,但是我不确定如何访问它。
下面是代码和JSON
文件。我在正确的轨道上吗?有人建议,如果我在main方法中使用该Feature
类,则可以访问该对象。例如ex:Feature obj = new Feature();
,然后使用obj.getType()
访问任何变量。我实际上想访问整个文件,以便它呈现出显示在WorldWind Map
上的数据。
{
"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);
}
}
这是我得到错误的地方
public static void main(String[] args) {
final AppFrame af = (AppFrame) start("World Wind JSON Network Viewer", AppFrame.class);
File selectedFile = jfc.getSelectedFile();
ObjectMapper objectMapper = new ObjectMapper();
FeatureCollection features = objectMapper.readValue(selectedFile, FeatureCollection.class);
}
jfc表示创建局部变量 readValue引发未处理的异常
答案 0 :(得分:0)
要使用GeoJSON JSON
负载,可以使用geojson-jackson版本的1.12库。要使用它,您只需将其添加到您的Maven
配置中:
<dependency>
<groupId>de.grundid.opendatalab</groupId>
<artifactId>geojson-jackson</artifactId>
<version>1.12</version>
</dependency>
从现在开始,您可以阅读以下JSON
配置:
import com.fasterxml.jackson.databind.ObjectMapper;
import org.geojson.FeatureCollection;
import java.io.File;
import java.io.IOException;
public class GeoJSON {
public static void main(String[] args) throws IOException {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper objectMapper = new ObjectMapper();
FeatureCollection features = objectMapper.readValue(jsonFile, FeatureCollection.class);
features.forEach(System.out::println);
}
}
上面的代码显示:
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);
}
另请参阅: