我尝试过多种方法来解析这些数据:
JSONObject hospital2 = hospital.getJSONObject("address");
if (!hospital2.isNull("hospital")){
String hospitalName = hospital2.getString("hospital");
String hospitalLat = hospital.getString("lat");
String hospitalLon = hospital.getString("lon");
看起来像这样 - >>> http://imgur.com/a/OloES 从JSON网址,但要么工作
我的代码atm就是这样:
public class HospActivity extends AppCompatActivity {
public void goBack(View view)
{
Intent i = new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
public class DownloadTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... params) {
String result="";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while(data != -1){
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hosp);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<String> hospitals = new ArrayList<String>();
DownloadTask task = new DownloadTask();
String result = null;
try {
result = task.execute("http://nominatim.openstreetmap.org/search?q=crete%5bhospitals%5d+greece&format=json&polygon=0&addressdetails=1&limit=500").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
try {
JSONArray array = new JSONArray(result);
String asef = "";
for(int i = 0; i < array.length(); i++)
{
JSONObject hospital = array.getJSONObject(i);
JSONObject hospital2 = hospital.getJSONObject("address");
if (!hospital2.isNull("hospital")){
String hospitalName = hospital2.getString("hospital");
String hospitalLat = hospital.getString("lat");
String hospitalLon = hospital.getString("lon");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
我是Android开发的新手。
我的JSON
,
[{
"place_id": "146977563",
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright",
"osm_type": "way",
"osm_id": "377010610",
"boundingbox": ["35.3648199", "35.3654346", "24.4711123", "24.472325"],
"lat": "35.3650817",
"lon": "24.4716895296637",
"display_name": "Koumoundourou, Rethymno, Rethymni Municipality, Rethymno Regional Unit, Region of Crete, Decentralized Administration of Crete, Greece",
"class": "amenity",
"type": "hospital",
"importance": 0.201,
"icon": "http:\/\/nominatim.openstreetmap.org\/images\/mapicons\/health_hospital.p.20.png",
"address": {
"road": "Koumoundourou",
"city": "Rethymno",
"county": "Rethymni Municipality",
"state_district": "Region of Crete",
"state": "Decentralized Administration of Crete",
"country": "Greece",
"country_code": "gr"
}
}]
答案 0 :(得分:0)
尝试:
JSONArray jsonarray = new JSONArray(result);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
JSONObject hospital2 = jsonobject.getJSONObject("address")
String hospitalName = hospital2.getString("hospital");
String hospitalLat = hospital.getString("lat");
String hospitalLon = hospital.getString("lon");
}
答案 1 :(得分:0)
查看此tutorial有关如何填充ListView的信息。只需创建一个包含您正在寻找的字符串的简单类,而不是您创建的教程中的类。