我已經解析了一些JSON數據,它的工作正常,只要我將它存儲在字符串變量中。如何解析JSON到一個int?
我的問題是我需要的ID在一個int varibable而不是字符串。 我試圖做一個演員int id = (int) jsonObj.get("");
但它給出了一個錯誤消息,我不能將一個對象轉換爲int。 所以我試圖用轉換:
String id = (String) jsonObj.get("id");
int value = Integer.parseInt(id);
但也未工作。哪裏不對。 JSON如何與int一起工作? 我的字符串工作得很好,只有當我嘗試使它們成爲一個int時出現問題。
這裏是我的代碼:
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()不與簡單的JSON – Josef
工作你是什麼意思?你的'id'字符串究竟是什麼? –
int id = Integer.parseInt(jsonObj.get(「id」));這一次日食想要添加一個強制轉換爲字符串。 – Josef