本文介绍了在Java中无法访问getJSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此JSON从网址:
I have this Json from URL:
{
"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.
因此,这将是功能 - >属性 - > name_en(就像对象列表)
So it would be features -> properties -> name_en (list of objects like that)
我试试这个:
JSONParser parser = new JSONParser();
Object obj = parser.parse(Json_str);
JSONObject jsonObject = (JSONObject) obj;
JSONArray jsonFeaturesArr = new JSONArray(jsonObject.getJSONArray("features"));
为了创建第一个JSON数组,但我甚至无法做到这一点。我得到的错误:
该方法getJSONArray(字符串)是未定义的类型的JSONObject
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
(我有同样的错误getJSONObject)。 STH必须失去了,我是一个Java / Android的新手。
(I have the same error for "getJSONObject"). Sth must be missing, I'm a java/android Newbie.
如果我解决错误我怎么去深入的Json?
If I solve error how do I go deeper into 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中无法访问getJSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!