2013-03-14 13 views
6

Ogni voce news ha tre cose:title, content e date.lettura JSON doppia matrice bidimensionale in Java

Le voci vengono recuperate da un database e mi piacerebbe leggerle nella mia applicazione utilizzando JSONObject e JSONArray. Tuttavia, non so come usare queste classi.

Qui è la mia stringa JSON:

[ 
    { 
     "news":{ 
     "title":"5th title", 
     "content":"5th content", 
     "date":"1363197493" 
     } 
    }, 
    { 
     "news":{ 
     "title":"4th title", 
     "content":"4th content", 
     "date":"1363197454" 
     } 
    }, 
    { 
     "news":{ 
     "title":"3rd title", 
     "content":"3rd content", 
     "date":"1363197443" 
     } 
    }, 
    { 
     "news":{ 
     "title":"2nd title", 
     "content":"2nd content", 
     "date":"1363197409" 
     } 
    }, 
    { 
     "news":{ 
     "title":"1st title", 
     "content":"1st content", 
     "date":"1363197399" 
     } 
    } 
] 
+0

Per iniziare, fare una classe chiamata Notizie con 3 campi: titolo, contenuto e data. –

+0

okay e poi ?? – Alex

+0

Leggi la documentazione su come usare parser java json. –

risposta

8

La stringa JSON è un JSONArray di JSONObject che poi contengono un interno JSONObject chiamato "news".

Prova questo per analizzarlo:

JSONArray array = new JSONArray(jsonString); 

for(int i = 0; i < array.length(); i++) { 
    JSONObject obj = array.getJSONObject(i); 
    JSONObject innerObject = obj.getJSONObject("news"); 

    String title = innerObject.getString("title"); 
    String content = innerObject.getString("content"); 
    String date = innerObject.getString("date"); 

    /* Use your title, content, and date variables here */ 
} 
+0

Grazie, funziona! :) – Alex

+1

@ JonathanHugh Sono felice che tu sia riuscito a trovare una risposta alla tua domanda. Ricorda che se una risposta fornita soddisfa le tue esigenze, dovresti accettare quella risposta facendo clic sul segno di spunta sotto le frecce di voto in modo che le persone che visualizzano questa domanda in futuro siano in grado di dire quale risposta ha risolto il problema. – MCeley

2

Prima di tutto, la struttura JSON non è l'ideale. Hai una matrice di oggetti, con ogni oggetto che contiene un singolo oggetto. Tuttavia, si poteva leggere in questo modo:

JSONArray jsonArray = new JSONArray (jsonString); 
int arrayLength = jsonArray.length(); 

for (int counter = 0; counter < arrayLength; counter ++) { 
    JSONObject thisJson = jsonArray.getJSONObject (counter); 

    // we finally get to the proper object 
    thisJson = thisJson.getJSONObject ("news"); 

    String title = thisJson.getString ("title"); 
    String content = thisJson.getString ("content"); 
    String date = thisJson.getString ("date"); 

} 

Tuttavia!

Si poteva fare meglio se si cambia il JSON per simile al seguente:

[ 
    { 
     "title": "5th title", 
     "content": "5th content", 
     "date": "1363197493" 
    }, 
    { 
     "title": "4th title", 
     "content": "4th content", 
     "date": "1363197454" 
    } 
] 

Poi, si potrebbe analizzare come segue:

JSONArray jsonArray = new JSONArray (jsonString); 
int arrayLength = jsonArray.length(); 

for (int counter = 0; counter < arrayLength; counter ++) { 
     // we don't need to look for a named object any more 
    JSONObject thisJson = jsonArray.getJSONObject (counter);  

    String title = thisJson.getString ("title"); 
    String content = thisJson.getString ("content"); 
    String date = thisJson.getString ("date"); 
} 
+0

La data sbagliata è una stringa che non si ottiene usando getLong(), controlla le virgolette – Pragnani

+1

"Il tuo json è cattivo" è un'opinione. L'array potrebbe contenere altri oggetti oltre a "news", quindi il formato del json è in realtà piuttosto descrittivo. – 323go

+0

@ 323go in realtà dice che è una matrice bidimensionale nel titolo e nella prima riga della domanda dice "Ogni notizia ha tre cose: titolo, contenuto e data". Penso che non capisca davvero la sintassi degli array e degli oggetti JSON, quindi ho cercato di aiutare con un esempio. – Shade

Problemi correlati