2012-04-08 10 views
8

sto controllando se un oggetto in una stringa JSON esiste utilizza questo:Verificando esiste subobject in JSON

JSONObject json = null; 

     try { 
      json = new JSONObject(myJsonString); 
     } catch (JSONException e) { e.printStackTrace(); } 


     if(json.has("myObject")) System.out.println("EXISTS"); 

     else System.out.println("DOESN'T EXIST"); 

Il problema si presenta quando si tenta di controllare se esiste un oggetto secondario. es:

...,"queue":{"building":{"q0":{"id":177779,... 

coda esiste sempre e costruzione anche, ma q0 non è sempre presente. Quindi, come posso verificare l'esistenza di q0? E c'è un modo per controllarlo usando la libreria Gson?

Grazie in anticipo!

risposta

11

Si può semplicemente provare e restituire null se la prova non è riuscita. Oppure puoi spezzare il tuo tentativo in piccoli pezzi per monitorare dove fallisce.

/** 
* This method will return the JSONObject q0, if it exists 
* If it doesn't exist it will return NULL 
* 
*/ 
private JSONObject getQZero(JSONObject json) 
{ 
    try 
    { 
     return json.getJSONObject("queue").getJSONObject("building").getJSONObject("q0"); 
    } 
    catch (JSONException e) 
    { 
     // This could be triggered either because there is no q0 
     // or because the JSON structure is different from what was expected. 
     return null; 
    }  
} 

Si potrebbe anche andare passo dopo passo, se si desidera stampare i registri per ogni livello;

/** 
* This method will show where your jsonparsing fails. 
* It will throw a JSONOException if the json is way different from what 
* was expected, and otherwise it will print a log of where the parsing 
* failed. 
*/ 
private JSONObject getQZero(JSONObject json) throws JSONException 
{  
    // Stop if no queue 
    if (! myObject.has("queue") 
    { 
     Log.d(TAG, "no queue!"); 
     return null; 
    } 

    JSONObject queue = myObject.getJSONObject("queue"); 

    // Stop if no building 
    if (! queue.has("building") 
    { 
     Log.d(TAG, "no building!"); 
     return null; 
    } 

    JSONObject building = queue.getJSONObject("building") 

    // Stop if no q0 
    if (! building.has("q0")) 
    { 
     Log.d(TAG, "no q0!"); 
     return null; 
    } 

    JSONObject q0 = building.getJSONObject("q0"); 
    // Q0 is returned here. If the method returned earlier, it returned NULL 
    // You could also do nested ifs, but the indentation gets crazy 
    return q0; 
} 
+2

Grazie mille! –

+1

@KrLx_roller - contento che abbia aiutato! –

1

Utilizzare le eccezioni a vostro vantaggio

try { 
     JSONObject i = json. getJSONObject("q0"); 
     // Is there do something 
    } catch (JSONException e) { 
     // Isn't there 
    } 

http://www.json.org/javadoc/org/json/JSONObject.html#getJSONObject(java.lang.String)

JSONException - Se la chiave non si trova o se il valore non è un JSONObject.

+0

non funziona ... E 'sempre buttare: 'alcun valore per q0' –

+0

Sembra q0 è un JSONObject e non un int. –

+0

@PeterAjtai Sì, è un JSONObject che voglio verificare la sua esistenza –

Problemi correlati