java - 使用java将java字符串转换为geojson

标签 java json leaflet geojson

我想将传单的字符串 json 转换为 geojson,这是我的 json

[
  {
    "longitude": "xxx",
    "latitude": "xxx",
    "kode_toko": "xxx",
    "nama_toko": "xxx",
    "postal_code": xxx
  },

等(我的json中有更多类似的内容)..

这是我将数据从数据库转换为 json 字符串的代码

List<Object> data = new ArrayList<>();
Items item = null;

while(rs.next()) {
  item = new Items("","","","",0);
  //json = rs.getString("t");
  item.latitude = rs.getString("latitude");
  item.longitude = rs.getString("longitude");
  item.code = rs.getString("code");
  item.name = rs.getString("name");
  item.postalcode = rs.getInt("postal_code");
  data.add(item);

  //System.out.println("Items: " +data);
  //System.out.println("Item " +item);
}       

//convert to json
GsonBuilder build = new GsonBuilder();
build.setPrettyPrinting();
Gson gson = build.create(); 
String json = gson.toJson(data); //convert ke string`

如何将这个json字符串数据转换为geojson数据? 先谢谢了

最佳答案

GeoJSON 是 JSON,因此无需构建从数据发布的 JSON,只需构建最终的 GeoJSON,

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [xxx, yyy]
  },
  "properties": {
    "kode_toko": "www",
    "nama_toko": "uuu",
    "postal_code": "vvv"
  }
}

顺便说一句,在坐标中,xxx是经度,yyy是纬度。

GeoJSON

关于java - 使用java将java字符串转换为geojson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60331833/

相关文章:

java - 如何填充 BST 并以 Inorder 方式打印它

java - Libgdx : Detect empty TextureRegion

json - 亚马逊社交网络 : JSON toasts to Windows Phones

javascript - 如何使用 JSON 数据同时更新多个标记位置?传单 JS

javascript - 弹出react传单图库

java - Android 应用程序停止工作

java - 使用 Java Scanner 从控制台读取很长的字符串需要时间吗?

javascript - 从 JSON 插入 HTML 选择标签选项

json - Phoenix : render JSON from a template

javascript - 如何使用 "this"调用事件内的函数