|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Dec 2003
Messaggi: 1759
|
[Java&JSON] gestire array
Ho una stringa in formato JSON fatta così:
Codice:
{"routes":
{
"route": [
{
"summary": {
"leaveat": "11:58",
"arriveby": "12:23",
"duration": "25 mins",
"transfers": 1,
"fareA": "1euro",
"fareC": "0.8euro"
},
"sections": [
{"section1": "Station1/11:58-11:59"},
{"section2": "Station2/12:06-12:23"},
{"section3": "Station3/x"}
]
},
{
"summary": {
"leaveat": "12:03",
"arriveby": "12:43",
"duration": "40 mins",
"transfers": 2,
"fareA": "1.4euro",
"fareC": "1euro"
},
"sections": [
{"section1": "Station1/12:03-12:20"},
{"section2": "Station2/12:21-12:25"},
{"section3": "Station3/12:28-12:32"},
{"section4": "Station4/x"}
]
}
]
}
}
Lo sto facendo per un'app android e sto usando il parser che mette a disposizione Codice:
jObject = new JSONObject(result);
JSONObject routeObject = jObject.getJSONObject("route");
JSONObject summaryObject = routeObject.getJSONObject("summary");
JSONObject sectionsObject = routeObject.getJSONObject("sections");
Come posso ciclare all'interno di summaryObject per stampare queste 2 righe? duration: 25 mins - transfers: 1 - fareA: 1euro - fareC: 0.8euro duration: 40 mins - transfers: 2 - fareA: 1.4euro - fareC: 1euro |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2780
|
route lo devi ottenere con getJSONArray (e così tutti gli altri array):
http://developer.android.com/referen...lang.String%29 |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Dec 2003
Messaggi: 1759
|
Ci sto provando da qualche ora, ma non mi riesce.
Mi diresti come fare? |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Dec 2003
Messaggi: 1759
|
Ok, forse non è il modo migliore, ma alla fine ci sono riuscito così
Codice:
jObject = new JSONObject(result);
JSONArray routeArray = jObject.getJSONArray("route");
for(int i=0;i<routeArray.length();i++) {
String durationJS = routeArray.getJSONObject(i).getJSONObject("summary").getString("duration");
int transfersJS = routeArray.getJSONObject(i).getJSONObject("summary").getInt("transfers");
String fareAJS = routeArray.getJSONObject(i).getJSONObject("summary").getString("fareA");
String fareCJS = routeArray.getJSONObject(i).getJSONObject("summary").getString("fareC");
String fareJS = fareAJS+"/"+fareCJS;
}
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 01:18.



















