java - 无法在 java 中访问 getJSONArray

标签 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...

我需要处理列表中的属性。

所以应该是 features -> properties -> 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?

提前感谢您的帮助。

最佳答案

尝试:

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 ...........
  }

关于java - 无法在 java 中访问 getJSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13269512/

相关文章:

php - 需要帮助将 JSON 数据从 MYSQL 数据库获取到 Android 应用程序

java - 如何通过构造函数传递数组值

java - 如何使用 Spring Boot、Gradle 和 JSP 构建项目

android - 具有排队、播放、停止、暂停、恢复的 AudioTrack 类

android - 使用 YouTube Android SDK 发生错误时自动跳过 youtube 中的视频

java - 线程中断替代方案?

json - 如何从API管理策略中的响应体获取值(value)?

ajax - MVC 5无效的JSON原语

java - 如何在 Firebase 数据库 (Android) 中计算子项数量

java - 不正确求和数组并在线程 "main"java.lang.ArrayIndexOutOfBoundsException : 100 中给出异常