java 无法在java中访问getJSONArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13269512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:04:33  来源:igfitidea点击:

Can't access getJSONArray in java

javaandroidjson

提问by baron_bartek

I have this Json from URL:

我有这个来自 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...

I need to put my hands on properties in a list.

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

So it would be features -> properties -> name_en (list of objects like that)

所以这将是 features -> properties -> name_en(类似的对象列表)

I try this:

我试试这个:

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

in order to create first Json Array, but I can't even do that. I get error: The method getJSONArray(String) is undefined for the type JSONObject

为了创建第一个 Json 数组,但我什至不能这样做。我收到错误:方法 getJSONArray(String) 未定义为 JSONObject 类型

(I have the same error for "getJSONObject"). Sth must be missing, I'm a java/android Newbie.

(我对“getJSONObject”有同样的错误)。一定是缺少某些东西,我是一个 java/android 新手。

If I solve error how do I go deeper into Json?

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

Thanx in advance for Help.

提前感谢帮助。

回答by ρяσ?ρ?я K

try as:

尝试如下:

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

回答by meh

It doesn't look like XML, but a JSON.

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

You should initialize a JSONObject with that string.

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

 JSONObject obj = new JSONObject(str);

And then to access a certain field just check if it exists, and then try to get a data from that field, for example to get the JSONArray call:

然后访问某个字段只需检查它是否存在,然后尝试从该字段获取数据,例如获取 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
  }
}

回答by baron_bartek

Workable code:

可用代码:

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);
    }