-1

Completely new to android and JSON usage. I tried to follow a few previous posts and answers but cannot get it to work. My result is of this form :

{
    "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
    }
}

I am trying to get the lat and longitude to show them on a map. I tried creating JSON Objects like this to get the first position :

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

Any help..

Selvin
  • 6,598
  • 3
  • 37
  • 43
  • You tell that you "cannot get it to work" what does that mean ? Is there an error ? Is the returned values incorrect ? Please be precise or we won't be able to help you. – WannaGetHigh Sep 15 '15 at 14:02
  • *Any help..* yes, learn basics ... searchResults is array use proper `JSONObject.getXXXXX()` method to get an array then iterate it ... as for double, you also have proper `JSONObject.getXXXXX()` method for this ... there are bazillions questions about parsing json in java here on SO ... if you can't learn from examples and need direct code for your specific JSON you should consider stop programming – Selvin Sep 15 '15 at 14:03

3 Answers3

0

Isnt searchresult an JSONArray? Your JSON Code is a lttle bit hard to read

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 surely is there no method .getJSONObject from JSONArray. My mistake for not editing this line first

Andriel
  • 26
  • 4
0

Use Gson. There is no need for you to reinvent tires for your just-invented-wheel.

See how they do it here https://github.com/lissyfeja/MapQuest/blob/master/src/de/bht/mmi/ema/EditEventActivity.java

Basically you create an object that is of a structure you get from the JSON, then you tell Gson to parse your string to that object structure.

mEvent = new Gson().fromJson(json, MQCalendarEvent.class);

Here is another good example, which just straight parses the data with default parser and then gets it with some simple methods Gson provides: Android - Parse JSON using GSON

Community
  • 1
  • 1
0

Try this:

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"));
}
Arvind
  • 444
  • 4
  • 14