1

My JSON:

{
    "adresses": [
        {
            "region": "Auvergne-Rhône-Alpes",
            "pertinence": 0.41,
            "cp": "73200",
            "coordonnee": [
                45.6681753,
                6.3863336
            ],
            "rue": "Rue Pasteur",
            "osm_id": "8063355",
            "pays": "France",
            "ville": "Albertville",
            "quartier": "Le Parstet",
            "nom": "",
            "secteur": "Albertville",
            "numero": ""
        },
        {
            "region": "Auvergne-Rhône-Alpes",
            "pertinence": 0.41,
            "cp": "73200",
            "coordonnee": [
                45.6635769,
                6.3830012
            ],
            "rue": "Rue Pasteur",
            "osm_id": "69253010",
            "pays": "France",
            "ville": "Albertville",
            "quartier": "",
            "nom": "",
            "secteur": "Albertville",
            "numero": ""
        },
        {
            "region": "Auvergne-Rhône-Alpes",
            "pertinence": 0.41,
            "cp": "73400",
            "coordonnee": [
                45.7469095,
                6.4237485
            ],
            "rue": "Rue Pasteur",
            "osm_id": "170250718",
            "pays": "France",
            "ville": "Ugine",
            "quartier": "",
            "nom": "",
            "secteur": "Albertville",
            "numero": ""
        }
    ]
}

I need to remove one of adresses when "osm_id" is equals to a variable.

I have started to put the JSON in a ArrayList string for delete the line I want then redo the JSON. But element "coordonnee" is an array, and i don't know who can i do.

Here my trial :

    JSONObject req = reponse.convertToJsonObject();
    JSONArray adresses = req.getJSONArray( "adresses" );
    ArrayList<String> tab = new ArrayList<String>();
    for ( int i = 0; i < adresses.length(); i++ ) {
    JSONObject adr = adresses.getJSONObject( i );
    String line = adr.getString( "region" ) + " " + adr.getDouble( "pertinence" ) + " "
    + adr.getString( "cp" ) + " " + adr.getString( "coordonnee" )
    + " " + adr.getString( "rue" ) + " " + adr.getString( "osm_id" ) + " "
    + adr.getString( "pays" ) + " " + adr.getString( "ville" )
     + " " + adr.getString( "quartier" ) + " " + adr.getString( "nom" ) + " "
    + adr.getString( "secteur" ) + " " + adr.getString( "numero" );
    tab.add( line );
}

Can you help me ?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Grichka
  • 53
  • 1
  • 11

1 Answers1

0

I think this should do what you want. It will filter out any addresses that have an "osm_id" matching your provided variable.

JSONObject req = reponse.convertToJsonObject();
JSONArray filteredAddresses = new JSONArray();
String filterId = "..."; // put whatever your ID is here

JSONArray unfilteredAddresses = req.getJSONArray("adresses");
for (int i = 0; i < unfilteredAddresses.length(); i++){
    JSONObject addressJson = unfilteredAddresses.getJSONObject(i);
    if (!addressJson.has("osm_id") || !addressJson.getString("osm_id").equals(filterId)){
        filteredAddresses.put(addressJson);
    }
}
req.put("adresses", filteredAddresses);
Ownaginatious
  • 787
  • 4
  • 14
  • Thank you for your answer, I test tomorrow and I will tell you if it works ! – Grichka Jul 27 '16 at 17:24
  • Hi, I tried your solution, but "req.getJSONArray("adresses")" is highlighted by eclpise with the following message : "change type of 'addressJson' to 'Object'. – Grichka Jul 28 '16 at 06:56
  • Sorry, it's been a while since I used the `org.json` library, so I'm a little rusty ;) I edited the answer. See if that works for you. – Ownaginatious Jul 28 '16 at 08:20