全新的android和JSON用法。我试着关注一些以前的帖子和答案,但无法让它发挥作用。 我的结果是这种形式:
{
"searchResults": [
{
"resultNumber": 1,
"distance": 1.4849,
"sourceName": "mqap.internationalpois",
"name": "Le Restaurant",
"shapePoints": [
-37.840523,
144.991425
],
"distanceUnit": "m",
"key": "97d091ae-1a7e-48c0-b1ab-ea3a26b2da6c",
"fields": {
"cats_ext": "(all) Restaurants::French",
"phone": "+(33)-(4)-68356262",
"side_of_street": "N",
"navsics": "581208",
"lng": 144.991425,
"city": "Perpignan",
"country": "FR",
"cats": "(all) Restaurants",
"id": "12500223531687",
"mqap_geography": {
"latLng": {
"lng": 144.991425,
"lat": -37.840523
}
},
"navsics_ext": "581208::58120849",
"address": "16 Cours Lazare Escarguel",
"postal_code": "66000",
"name": "Le Restaurant",
"mqap_id": "97d091ae-1a7e-48c0-b1ab-ea3a26b2da6c",
"disp_lat": -37.840523,
"street_link_id": "",
"lat": -37.840523,
"disp_lng": 144.991425
}
},
{
"resultNumber": 2,
"distance": 11.4161,
"sourceName": "mqap.internationalpois",
"name": "Le Val d'Or",
"shapePoints": [
-37.881779,
145.167267
],
"distanceUnit": "m",
"key": "acf9c655-571e-4129-b70f-2e71f64894c2",
"fields": {
"cats_ext": "(all) Restaurants::French",
"phone": "+(33)-(3)-85451370",
"side_of_street": "N",
"navsics": "581208",
"lng": 145.167267,
"city": "Mercurey",
"country": "FR",
"cats": "(all) Restaurants",
"id": "12500223506348",
"mqap_geography": {
"latLng": {
"lng": 145.167267,
"lat": -37.881779
}
},
"navsics_ext": "581208::58120849",
"address": "140 Grande Rue",
"postal_code": "71640",
"name": "Le Val d'Or",
"mqap_id": "acf9c655-571e-4129-b70f-2e71f64894c2",
"disp_lat": -37.881779,
"street_link_id": "",
"lat": -37.881779,
"disp_lng": 145.167267
}
},
{
"resultNumber": 3,
"distance": 14.9848,
"sourceName": "mqap.internationalpois",
"name": "Le Saint Germain",
"shapePoints": [
-37.967893,
145.179022
],
"distanceUnit": "m",
"key": "66ad361d-e4c2-4e61-bfc3-dda863d84418",
"fields": {
"cats_ext": "(all) Restaurants",
"phone": "+(33)-(4)-67097575",
"side_of_street": "N",
"navsics": "581208",
"lng": 145.179022,
"city": "Pézenas",
"country": "FR",
"cats": "(all) Restaurants",
"id": "12500223648887",
"mqap_geography": {
"latLng": {
"lng": 145.179022,
"lat": -37.967893
}
},
"navsics_ext": "581208",
"address": "6 Avenue Paul Vidal de La Blache",
"postal_code": "34120",
"name": "Le Saint Germain",
"mqap_id": "66ad361d-e4c2-4e61-bfc3-dda863d84418",
"disp_lat": -37.967893,
"street_link_id": "",
"lat": -37.967893,
"disp_lng": 145.179022
}
}
],
"origin": {
"latLng": {
"lng": 144.970242,
"lat": -37.827057
},
"postalCode": "",
"adminArea5Type": "City",
"adminArea4": "",
"adminArea5": "",
"adminArea4Type": "County",
"street": "",
"adminArea1Type": "Country",
"adminArea1": "",
"adminArea3": "",
"adminArea3Type": "State"
},
"resultsCount": 3,
"hostedData": [
{
"tableName": "mqap.internationalpois",
"extraCriteria": "navsics='581208'",
"parameters": [
"581208"
],
"columnNames": []
}
],
"totalPages": 1,
"info": {
"statusCode": 0,
"copyright": {
"text": "© 2015 MapQuest, Inc.",
"imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
"imageAltText": "© 2015 MapQuest, Inc."
},
"messages": []
},
"options": {
"kmlStyleUrl": "http://www.search.mapquestapi.com/kml-default.kml",
"shapeFormat": "raw",
"ambiguities": true,
"pageSize": 3,
"radius": 25,
"currentPage": 1,
"units": "m",
"maxMatches": 30
}
}
我正在尝试将经纬度显示在地图上。 我尝试像这样创建JSON对象以获得第一个位置:
JSONObject mainObject = new JSONObject(resmsg);
JSONObject searchResults = mainObject.getJSONObject("searchResults");
JSONObject mqap_geography = searchResults.getJSONObject("mqap_geography");
JSONObject latLng = mqap_geography.getJSONObject("latLng");
Double lat = Double.parseDouble(latLng.getJSONObject("lat").toString());
Double lon = Double.parseDouble(latLng.getJSONObject("lng").toString());
任何帮助..
答案 0 :(得分:0)
Isnt searchresult是否是JSONArray? 您的JSON代码有点难以阅读
JSONObject mainObject = new JSONObject(resmsg);
JSONArray searchResults = mainObject.getJSONArray("searchResults");
...
--> Iterate over searchResults to get the JSONObject you need e.g.mqap_geography
....
JSONObject latLng = mqap_geography.getJSONObject("latLng");
Double lat = Double.parseDouble(latLng.getJSONObject("lat").toString());
Double lon = Double.parseDouble(latLng.getJSONObject("lng").toString());
@Edit肯定没有来自JSONArray的方法.getJSONObject。我错误的是不先编辑这一行
答案 1 :(得分:0)
使用Gson。没有必要为你刚刚发明的车轮重新发明轮胎。
了解他们如何在https://github.com/lissyfeja/MapQuest/blob/master/src/de/bht/mmi/ema/EditEventActivity.java
进行操作基本上,您创建的对象是您从JSON获得的结构,然后您告诉Gson将您的字符串解析为该对象结构。
mEvent = new Gson().fromJson(json, MQCalendarEvent.class);
这是另一个很好的例子,它只是使用默认解析器直接解析数据,然后使用Gson提供的一些简单方法来获取它:Android - Parse JSON using GSON
答案 2 :(得分:0)
试试这个:
JSONObject mainObject = new JSONObject(resmsg);
JSONArray searchResults = mainObject.getJSONArray("searchResults");
for(int i=0 ;i <searchResults.lenth() ; i++)
{
JSONObject searchObject = searchResults.getJSONObject(i);
JSONObject objectField = searchObject.getJSONObject("fields");
JSONObject mqap_geography = objectField.getJSONObject("mqap_geography");
JSONObject latLng = mqap_geography.getJSONObject("latLng");
Double lat = Double.parseDouble(latLng.getString("lat"));
Double lon = Double.parseDouble(latLng.getString("lng"));
}