Jump to content

traitement de données JSON


Recommended Posts

Bonjour à tous,

Je coince sur un truc : j'ai des données au format JSON de ce type :


{ "data" : {
  "id" : 1,
  "nom" : "Norris",
  "prenom" : "Chuck"
},
 "result" : "Informations sur l'utilisateur",
 "status" : 200
}

Mais je ne connais pas forcément le contenu de "data" (ni les clés, ni les valeurs).

Je voudrais parcourir le JSONObject "data" et afficher la clé et la valeur.

Un truc de ce genre (code totalement fictif ;) ) :

jObject = new JSONObject(string_des_donnee_json);

JSONObject dataItem = jObject.getJSONObject("data");

for (int i = 0; i < dataItem.lenthg(); i++) {

Log.i(dataItem.getKey());

Log.i(dataItem.getValue());

}

Si vous avez des idées, je suis preneur !

Merci d'avance !

Link to comment
Share on other sites

Bon, en fait j'ai trouvé :)

		jObject = new JSONObject(string_des_donnee_json);
		// les infos
		if(!jObject.isNull("data")) {
			JSONObject dataItem = jObject.getJSONObject("data");
			if(dataItem.length()>0) {
		   	 //Map<String,String> map = new HashMap<String,String>();
   			 Iterator iter = dataItem.keys();
   			 while(iter.hasNext()){
   				 String key = (String)iter.next();
   				 String value = (String)dataItem.getString(key).toString();
   				 //map.put(key,dataItem.getString(key));	   				
   			 }
			}
		}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...