Voglio leggere questo file JSON
con java usando la libreria json simple.
Il mio file JSON
ha questo aspetto:
[
{
"name":"John",
"city":"Berlin",
"cars":[
"audi",
"bmw"
],
"job":"Teacher"
},
{
"name":"Mark",
"city":"Oslo",
"cars":[
"VW",
"Toyata"
],
"job":"Doctor"
}
]
Questo è il codice java che ho scritto per leggere questo file:
package javaapplication1;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JavaApplication1 {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("c:\\file.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");
System.out.println(name);
String city = (String) jsonObject.get("city");
System.out.println(city);
String job = (String) jsonObject.get("job");
System.out.println(job);
// loop array
JSONArray cars = (JSONArray) jsonObject.get("cars");
Iterator<String> iterator = cars.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Ma ottengo la seguente eccezione:
Eccezione nel thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject at javaapplication1.JavaApplication1.main(JavaApplication1.java:24)
Qualcuno può dirmi cosa sto facendo di sbagliato? L'intero file è un array e ci sono oggetti e un altro array (auto) nell'intero array del file. Ma non so come posso analizzare l'intero array in un array java. Spero che qualcuno possa aiutarmi con una linea di codice che mi manca nel mio codice.
Grazie
L'intero file è un array e ci sono oggetti e altri array (per esempio auto) nell'intero array del file.
Come dici tu, lo strato più esterno del tuo blob JSON è un array. Pertanto, il tuo parser restituirà un JSONArray
. Puoi quindi ottenere dei JSONObject
dall'array ...
JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
String name = (String) person.get("name");
System.out.println(name);
String city = (String) person.get("city");
System.out.println(city);
String job = (String) person.get("job");
System.out.println(job);
JSONArray cars = (JSONArray) person.get("cars");
for (Object c : cars)
{
System.out.println(c+"");
}
}
Per riferimento, vedere "Esempio 1" nella pagina esempio di decodifica json-simple.
Potete usare la libreria Jackson e semplicemente usare queste 3 righe per convertire il vostro file json in un oggetto Java.
ObjectMapper mapper = new ObjectMapper();
InputStream is = Test.class.getResourceAsStream("/test.json");
testObj = mapper.readValue(is, Test.class);
Potete usare Gson per questo.
GSON
è una libreria Java che può essere usata per convertire gli oggetti Java nella loro rappresentazione JSON
. Può anche essere usata per convertire una stringa JSON
in un oggetto Java equivalente.
Date un'occhiata a questo Convertire JSON in Java