parsing json problème parsing
Bonjour,
Comment faire pour pouvoir parser ce fichier .json?
j'ai implémenté ce code mais j'ai une erreur:
05-16 12:31:39.929: W/System.err(8783): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
Gson gson = new Gson();
Reader r = new InputStreamReader(getJSONData(url));
JsonParser parser = new JsonParser();
JsonArray Jarray = parser.parse(r).getAsJsonArray();
ArrayList<TempAttente> lcs = new ArrayList<TempAttente>();
for(JsonElement obj : Jarray )
{
TempAttente cse = gson.fromJson( obj , TempAttente.class);
lcs.add(cse);
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
public class TempAttente {
public String sens,terminus,infotrafic,temps;
private List<Ligne> ligne;
private List<Arret> arret;
public String getSens() {
return sens;
}
public void setSens(String sens) {
this.sens = sens;
}
public String getTerminus() {
return terminus;
}
public void setTerminus(String terminus) {
this.terminus = terminus;
}
public String getInfotrafic() {
return infotrafic;
}
public void setInfotrafic(String infotrafic) {
this.infotrafic = infotrafic;
}
public String getTemps() {
return temps;
}
public void setTemps(String temps) {
this.temps = temps;
}
public List<Ligne> getLigne() {
return ligne;
}
public void setLigne(List<Ligne> ligne) {
this.ligne = ligne;
}
public List<Arret> getArret() {
return arret;
}
public void setArret(List<Arret> arret) {
this.arret = arret;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class Ligne {
public String numLigne,typeLigne;
public String getTypeLigne() {
return typeLigne;
}
public void setTypeLigne(String typeLigne) {
this.typeLigne = typeLigne;
}
public String getNumLigne() {
return numLigne;
}
public void setNumLigne(String numLigne) {
this.numLigne = numLigne;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public class Arret {
public String codeArret;
public String getCodeArret() {
return codeArret;
}
public void setCodeArret(String codeArret) {
this.codeArret = codeArret;
}
} |
fichier .json:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
[
{
"sens": 1,
"terminus": "Bout des Landes",
"infotrafic": false,
"temps": "0 mn",
"ligne": {
"numLigne": "32",
"typeLigne": 3
},
"arret": {
"codeArret": "CRQU4"
}
},
{
"sens": 1,
"terminus": "Orvault Gd Val",
"infotrafic": false,
"temps": "1 mn",
"ligne": {
"numLigne": "2",
"typeLigne": 1
},
"arret": {
"codeArret": "CRQU1"
}
}
] |
Merci.