【发布时间】:2018-02-08 18:37:15
【问题描述】:
我在一个项目中工作,从 URL 检索 JSON 后,我需要操作 JSON 文件。我从以下链接检索了我的 JSON 文件:
http://api.openeventdatabase.org/event/b2e7df60-3f25-4d80-b7ac-cffc10dd5313
该 JSON 文件包含有关法国特定服务站的信息,例如:
- 车站服务详情(街道、姓名、标记...)
- 服务(洗衣、厕所 ....)
- 最重要的是!近期燃料价格
为此,我尝试按照其他用户在 stackoverflow 上给出的解决方案来解决类似的问题。你可以看到:
Android JSON parsing of multiple JSONObjects inside JSONObject
这是我的代码(完整方法):
@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;
}
你能帮我找出错误吗?请查看第一个链接,其中包含 JSON 响应。
【问题讨论】:
-
将其移至法语堆栈溢出可能会有所帮助