使用org.json无序结构解析JSONArray

时间:2014-12-04 18:48:06

标签: android json loops geojson org.json

大家好我想问一下如何解析用org.json获取JSONArray值?

我有像这样的geojson数据

{
"features": [
    {
  "geometry": {
    "coordinates": [
      112.732816,
      -7.246557
    ],
    "type": "Point"
  },
  "type": "Feature",
  "properties": {
    "tinggi_tower": 80,
    "nm_type": "SST (4 kaki)",
    "site_id": "SBYPASTUR",
    "nama_site": "Pasar Turi Utara",
    "nm_kota": "Surabaya",
    "nm_site_type": "GF",
    "jml_tenant": 2
  }
}
],
"type": "FeatureCollection"
}

我希望获得长度和经度值,而不是像我这样尝试解析org.json库

limap = new ArrayList<Site_Map_Client>();
JSONArray arrfeatures = json.getJSONArray("features");
    for (int i = 0; i < arrfeatures.length(); i++) {
        Site_Map_Client modmap = new Site_Map_Client();
        JSONObject objfeatures = arrfeatures.getJSONObject(i);
        JSONObject objgeo = objfeatures.getJSONObject("geometry");
        JSONArray arrcoord = objgeo.getJSONArray("coordinates");
            //how to get longtitude and latitude value for this loop
            for (int j = 0; j < arrcoord.length(); j++) {
                if(i == 0){
                    modmap.setLongtitude(arrcoord.getDouble(i));
                } else if (i == 1) {
                    modmap.setLatitude(arrcoord.getDouble(i));
                }
            }
            JSONObject objprop = objfeatures.getJSONObject("properties");
            modmap.setSiteid(objprop.getString("site_id"));
            modmap.setNmsite(objprop.getString("nama_site"));
            modmap.setTipetower(objprop.getString("nm_type"));
            modmap.setTipesite(objprop.getString("nm_site_type"));
            modmap.setTinggitower(objprop.getDouble("tinggi_tower"));
            modmap.setKota(objprop.getString("nm_kota"));
            modmap.setJmtenant(objprop.getInt("jml_tenant"));

            limap.add(modmap);
        }

问题是为什么长度和长度结构混乱,我的坐标错误? 在本案例研究中迭代JsonArray的想法是什么?

抱歉我的英文不好,谢谢

2 个答案:

答案 0 :(得分:0)

啊,对不起,我错了看循环

迭代必须像这样

for (int j = 0; j < arrcoord.length(); j++) {
            if(j == 0){
                modmap.setLongtitude(arrcoord.getDouble(j));
            } else if (j == 1) {
                modmap.setLatitude(arrcoord.getDouble(j));
            }
        }

现在已经解决了

答案 1 :(得分:0)

你的内部for循环应该看起来像这样

for (int j = 0; j < arrcoord.length(); j++) {
      if(j == 0){
         modmap.setLongtitude(arrcoord.getDouble(i));
      } else if (j == 1) {
         modmap.setLatitude(arrcoord.getDouble(i));
      }
}

在if语句中使用j变量而不是i