Prova con questo:
JSONObject j_user = jObj.getJSONObject("user");
String uid = j_user.getString("uid");
...
//And so on with the rest of the fields
Domanda
Quindi ricevo un array JSON in un httpresponse. L'array è costituito dai seguenti oggetti/variabili.
{"sessid": "VXKexkmbumbByesrlvaxrxsaftfhdqd8", "session_name": "sess88cdfb2f1c420898", "utente": {"uid": "60", "nome": "ABC", "tema": "", firma ":": " "," Signature_Format ":" Filtered_html "," Creato ":" 13082976 "," Access ":" 1386287 "," Login ": 1386211," Status ":" 1 "," Timezone ": Null," Language ": "", "immagine": null, "dati": {"mimemail_textonly": 0}, "ruoli": {"2": "utente autenticato", "5": "utente centrale"}, "field_centre_reference": { "und": [{"nid": "256"}]}, "field_first_name": {"und": [{"valore": "web", "formato": null, "safe_value": "web"} ]}, "field_surname": {"und": [{"valore": "servizi", "formato": null, "safe_value": "servizi"}]}, "bounce_mail_blocked": false, "force_password_change": " 0 "}}
Ora voglio ricevere tutti questi oggetti/stringhe in variabili separate. Come se volessi archiviare il "sessid" in una stringa variabile session_id. E così via. Posso ottenere i primi due (IE Sessid e Session_name) in modo semplice con l'aiuto del seguente codice.
response = client.execute(httppost);
BasicResponseHandler handler = new BasicResponseHandler();
String data = handler.handleResponse(response);
jObj = new JSONObject(data);
sessid = jObj.getString("sessid");
Log.d("sessid obj", sessid);
session_name = jObj.getString("session_name");
Log.d("session_name", session_name);
Ma dal momento che sono un noob ad Android, non so come far salvare il resto dei dati nelle variabili. I dati imminenti non possono essere salvati in modo semplice.
Soluzione
Prova con questo:
JSONObject j_user = jObj.getJSONObject("user");
String uid = j_user.getString("uid");
...
//And so on with the rest of the fields
Altri suggerimenti
{ // json object node
"sessid": "vxkEXkMBUmBByESRlvaxrxSaFTfhDqd8",
"session_name": "SESS88cdfb2f1c420898",
"user": { // json object user
"uid": "60", // string
"name": "abc",
"theme": "",
"signature": "",
"signature_format": "filtered_html",
"created": "13082976",
"access": "1386287",
"login": 1386211,
"status": "1",
"timezone": null,
"language": "",
"picture": null,
"data": { // json object data
"mimemail_textonly": 0 //string
},
....// rest of the json
{
Rappresenta il nodo oggetto JSON
[
Rappresenta il nodo Array JSON
Analizzare
JSONObject jObj = new JSONObject(load());
String sessid = jObj.getString("sessid");
Log.d("sessid obj", sessid);
JSONObject user = jObj.getJSONObject("user");
String uid = user.getString("uid");
Log.d("sessid obj", uid);
Per analizzare i dati
"data": {
"mimemail_textonly": 0
},
JSONObject data= user.getJSONObject("data");
Log.i(".......",""+data.getString("mimemail_textonly"));
A parser field_centre_reference
"field_centre_reference": { // json object field_centre_reference
"und": [ // json array und
{ // json object node
"nid": "256" //string
}
]
},
JSONObject field= user.getJSONObject("field_centre_reference");
JSONArray jr = field.getJSONArray("und");
JSONObject jb1 = (JSONObject) jr.get(0);
Log.i(".......",""+jb1.getString("nid"));
Dai un'occhiata a JacksonParser Library, fornisce annotazioni che rendono il tuo lavoro così facile. Ed è una delle librerie di analisi più veloce ... puoi averlo qui
Modificare:Ad esempio, puoi dare un'occhiata a una delle mie domande precedenti JacksonParser Databind e Core Cause "File duplicato trovato per APK"?
Dovresti notare che il JSON che hai ricevuto è un JSON nidificato. È come una mappa che uno dei valore della mappa è anche una mappa. Nel tuo caso, c'è un JSONObject contiene sessid e session_name mentre è solo stringa, ma "utente" è anche un JSONObject, quindi puoi analizzarlo a livello per livello. Per maggiori dettagli, puoi leggere http://www.vogella.com/articles/androidjson/article.html
Prova questo..
Ottenere
"user": {
"uid": "60",
JSONObject obj_user = jObj.getJSONObject("user");
String uid = obj_user.getString("uid");
"user": {
"uid": "60",
"data": {
"mimemail_textonly": 0
}
JSONObject obj_data = jObj.getJSONObject("data");
String mimemail_textonly = obj_user.getString("mimemail_textonly");
JSONObject obj_roles = jObj.getJSONObject("roles");
String two = obj_roles.getString("2");
"field_centre_reference": {
"und": [
{
"nid": "256"
JSONObject obj_field_centre_reference = jObj.getJSONObject("field_centre_reference");
JSONArray ary_und = obj_field_centre_reference.getJSONArray("und");
JSONObject objve = ary_und.getJSONObject(0);
String nid= objve.getString("nid");