我的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": ""
}
]
}
当“osm_id”等于变量时,我需要删除其中一个地址。
我已经开始将JSON放在ArrayList字符串中以删除我想要的行然后重做JSON。 但元素“coordonnee”是一个数组,我不知道我该怎么做。
这是我的试用版:
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 );
}
你能帮助我吗?
答案 0 :(得分:0)
我认为这应该做你想要的。它将过滤掉任何与您提供的变量匹配的“osm_id”的地址。
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);