I'm working in a project where after retrieving JSON from a URL, I need to manipulate the JSON file. I retrieved my JSON file from the following link:
http://api.openeventdatabase.org/event/b2e7df60-3f25-4d80-b7ac-cffc10dd5313
That JSON file contains information on a specific Service Station in France such as:
- The Station Service details (steet,name,mark ...)
- The Services (Washing,WC ....)
- And the most importantly! The recent price of fuels
For that, I've tried to follow a solution given here at stackoverflow, by other user, to a similar problem. You can see:
Android JSON parsing of multiple JSONObjects inside JSONObject
Here is my code (full method):
@GET
@Path("/DecouvrirJSONInfo/{stationServiceInfo}")
public StationService DecouvrirJSON(@PathParam ("stationServiceInfo") String jsonString) throws JSONException
{
JSONObject myResponse = new JSONObject(jsonString);
String nom;
String carburantGazole, carburantSP95, carburantSP98, carburantGPL;
float prixCarburantGazole, prixCarburantSP95, prixSP98, prixGPL;
Date dtActGazole, dtActSP98, dtActSP95,dtActGPL;
StationService actuelStationService = new StationService();
Carburant carburant = new Carburant();
List<Carburant> carburants = new ArrayList<Carburant>();
try
{
//nom = myResponse.getString("nom");
Iterator<String> keysRoot = myResponse.keys();
while (keysRoot.hasNext())
{
String rootKey = keysRoot.next();
if(rootKey == "properties")
{
JSONObject innerZeroJObject = myResponse.getJSONObject(rootKey);
Iterator<String> keys = innerZeroJObject.keys();
while( keys.hasNext() )
{
String key = keys.next();
//Log.v("**********", "**********");
//Log.v("category key", key);
if(key=="carburant")
{
JSONObject innerJObject = myResponse.getJSONObject(key);
Iterator<String> innerKeys = innerJObject.keys();
while( innerKeys.hasNext() )
{
String innerKkey = keys.next();
if(innerKkey == "1") // gazole
{
JSONObject innerIIJObject = myResponse.getJSONObject(innerKkey);
Iterator<String> innerIKeys = innerIIJObject.keys();
while( innerIKeys.hasNext() )
{
carburantGazole = innerIIJObject.getString("carburant");
dtActGazole = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS")
.parse(innerIIJObject.getString("maj"));
prixCarburantGazole = Float.parseFloat( innerIIJObject.getString("prix"));
carburant.DefinirNomCarburant(carburantGazole);
carburant.DefinirPrixCarburant(prixCarburantGazole);
carburants.add(carburant);
carburant = new Carburant();
}
}
else if(innerKkey == "2") // Sp95
{
JSONObject innerIIJObject = myResponse.getJSONObject(innerKkey);
Iterator<String> innerIKeys = innerIIJObject.keys();
while( innerIKeys.hasNext() )
{
carburantSP95 = innerIIJObject.getString("carburant");
dtActSP95 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS")
.parse(innerIIJObject.getString("maj"));
prixCarburantSP95 = Float.parseFloat( innerIIJObject.getString("prix"));
carburant.DefinirNomCarburant(carburantSP95);
carburant.DefinirPrixCarburant(prixCarburantSP95);
carburants.add(carburant);
carburant = new Carburant();
}
}
else if(innerKkey == "3") // SP98
{
JSONObject innerIIJObject = myResponse.getJSONObject(innerKkey);
Iterator<String> innerIKeys = innerIIJObject.keys();
while( innerIKeys.hasNext() )
{
carburantSP98 = innerIIJObject.getString("carburant");
dtActSP98 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS")
.parse(innerIIJObject.getString("maj"));
prixSP98 = Float.parseFloat( innerIIJObject.getString("prix"));
carburant.DefinirNomCarburant(carburantSP98);
carburant.DefinirPrixCarburant(prixSP98);
carburants.add(carburant);
carburant = new Carburant();
}
}
//String value = innerJObject.getString(innerKkey);
//Log.v("key = "+key, "value = "+value);
}
}
}
}
}
//actuelStationService.DefinirNomStationService(nom);
actuelStationService.DefinirCarburants(carburants);
}
catch(Exception ex)
{
}
return actuelStationService;
}
Can you help me and find the error(s). Please see the first link, which contains the JSON response.