无法在java中访问getJSONArray

时间:2012-11-07 12:09:40

标签: java android json

我从URL获得了这个Json:

{
  "type":"FeatureCollection",
  "features":
    [
      {
        "type":"Feature",
        "properties":
          [
            {
              "type":"colliers",
              "thumb":"upload\/estate\/135\/thumb_1. Prologis Park Wroclaw I.jpg",
              "name_pl":"Prologis Park Wroc\u0142aw I",
              "name_en":"Prologis Park Wroc\u0142aw I",
              "completearea":"167 000",
              "completeareaunit":"m2",
              "workingarea":"",
              "workingareaunit":"m2",
              "id_type":"3",
              "id":"135",
              "lon":16.939201369628,
              "lat":51.037378299619,
              "images":["public\/upload\/estate\/135\/1. Prologis Park Wroclaw I.jpg"]
            }
          ],
        "geometry":
          {
            "type":"Point",
            "coordinates":[16.939201369628,51.037378299619]
          },
        "crs":
          {
            "type":"name",
            "properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}
          }
        },

      {
        "type":"Feature",
        "properties":
          [
            {
              "type":"colliers",
              "thumb":"upload\/estate\/136\/thumb_2. Prologis Park Wroclaw III.jpg",
              "name_pl":"Prologis Park Wroc\u0142aw III",
              "name_en":"Prologis Park Wroclaw III",
              "completearea":"129 500",
              "completeareaunit":"m2",
              "workingarea":"",
              "workingareaunit":"m2",
              "id_type":"3",
              "id":"136",
              "lon":16.928386702881,
              "lat":51.105440250407,
              "images":
                [
                  "public\/upload\/estate\/136\/2. Prologis Park Wroclaw III.jpg"
                ]
            }
          ],
        "geometry":
          {
            "type":"Point",
            "coordinates":[16.928386702881,51.105440250407]
          },
        "crs":
          {
            "type":"name",
            "properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}
          }
        },
        .................... more more more...

我需要把手放在列表中的属性上。

所以这将是功能 - >属性 - > name_en(类似的对象列表)

我试试这个:

JSONParser parser = new JSONParser();
Object obj = parser.parse(Json_str);    
JSONObject jsonObject = (JSONObject) obj;
JSONArray jsonFeaturesArr = new JSONArray(jsonObject.getJSONArray("features")); 

为了创建第一个Json数组,但我甚至不能这样做。我收到错误: 对于类型JSONObject

,方法getJSONArray(String)未定义

(我对“getJSONObject”有同样的错误)。某事必须缺失,我是一个java / android新手。

如果我解决了错误,我该如何更深入地了解Json?

提前帮助Thanx。

3 个答案:

答案 0 :(得分:6)

尝试:

JSONObject jSONObject = new JSONObject(jsonString);
String str_type=jSONObject.getString("type");

// using JSONArray
JSONArray featuresArr = jSONObject.getJSONArray("features");
  for (int i=0; i<featuresArr.length; i++){
    JSONObject anotherjsonObject = featuresArr.getJSONObject(i);
    //access the fields of that json object
    String str_type_one=anotherjsonObject.getString("type");
   JSONArray featuresArr_properties = anotherjsonObject.getJSONArray("properties");

   JSONObject propertiesjsonObject = featuresArr_properties.getJSONObject(0);
   String str_type=propertiesjsonObject.getString("type");
   String str_type=propertiesjsonObject.getString("thumb");
   String str_type=propertiesjsonObject.getString("name_pl");
   String str_type=propertiesjsonObject.getString("name_en");
 ////parse all items ...........
  }

答案 1 :(得分:1)

它看起来不像XML,而是JSON。

您应该使用该字符串初始化JSONObject。

 JSONObject obj = new JSONObject(str);

然后访问某个字段只检查它是否存在,然后尝试从该字段获取数据,例如获取JSONArray调用:

if (obj.has("features")&&!obj.isNull("features")){
  JSONArray array = obj.getJSONArray("");
  for (int i=0; i<array.length; i++){
    JSONObject anotherObject = array.getJSONObject(i);
    //access the fields of that json object
  }
}

答案 2 :(得分:0)

可行的代码:

import org.json.JSONArray;
import org.json.JSONObject;

    try {

        JSONObject jSONObject = new JSONObject(Json_str);
        String str_type=jSONObject.getString("type");

        JSONArray featuresArr = jSONObject.getJSONArray("features");
          for (int i=0; i<featuresArr.length(); i++)
          {
            JSONObject anotherjsonObject = featuresArr.getJSONObject(i);
            String str_type_one=anotherjsonObject.getString("type");
           JSONArray featuresArr_properties = anotherjsonObject.getJSONArray("properties");

           JSONObject propertiesjsonObject = featuresArr_properties.getJSONObject(0);
           str_type=propertiesjsonObject.getString("name_pl");
          }         

    } catch (Exception e) {
        e.printStackTrace();
        Log.i("Jsor parser error","Oh no!"  + e);
    }