Ho analizzato alcuni dati JSON e funziona bene fino a quando lo memorizzo in variabili String.Come analizzare JSON in un int?
Il mio problema è che ho bisogno dell'ID in un varibable int e non in String. ho provato a fare un cast int id = (int) jsonObj.get("");
Ma dà un messaggio di errore che non riesco a convertire un oggetto in un int. così ho cercato di convertire utilizzando:
String id = (String) jsonObj.get("id");
int value = Integer.parseInt(id);
Ma anche che non funziona. Che c'è. Come funziona JSON con int? Le mie stringhe stanno funzionando bene solo quando provo a renderle come int ho problemi.
Ecco il mio codice:
public void parseJsonData() throws ParseException {
JSONParser parser = new JSONParser();
Object obj = parser.parse(jsonData);
JSONObject topObject = (JSONObject) obj;
JSONObject locationList = (JSONObject) topObject.get("LocationList");
JSONArray array = (JSONArray) locationList.get("StopLocation");
Iterator<JSONObject> iterator = array.iterator();
while (iterator.hasNext()) {
JSONObject jsonObj = (JSONObject) iterator.next();
String name =(String) jsonObj.get("name");
String id = (String) jsonObj.get("id");
Planner.getPlanner().setLocationName(name);
Planner.getPlanner().setArrayID(id);
}
}
getInt() non funziona con una semplice JSON – Josef
Cosa vuoi dire? Cosa c'è esattamente nella stringa 'id'? –
int id = Integer.parseInt (jsonObj.get ("id")); Questa eclissi vuole aggiungere un cast a String. – Josef