我嘗試從JSON數據中獲取整數。但每當方法被調用,我得到的錯誤:從Java中的JSON數據獲取整數
Undeterminated object at character 16 of {"lat": 47175650
這是我的JSON數據:[{"lat": 47175650, "lon": 7637853}]
這裏是我的代碼來讀取數據。它是一個Android應用程序的它從一個文件中獲取數據,將所有對象從JSON數組轉換成一個字符串數組和有對象應該創造儘可能多的GeoPoints:
public void loadData(){
File f = new File("/data/data/com.example.app/files/file.txt");
if (f.exists()) {
String locations = null;
try {
FileInputStream loadLoc = openFileInput("file.txt");
List<Byte> data = new ArrayList<Byte>();
while (true) {
int b = loadLoc.read();
if (b == -1)
break; // end of file
else
data.add((byte) b);
}
// bytes to string
byte[] bytes = new byte[data.size()];
for (int i = 0; i<bytes.length; i++)
bytes[i] = data.get(i);
locations = new String(bytes);
Log.e("debug", locations);
loadLoc.close();
} catch (Exception ex) {
Log.e("debug", ex.getMessage());
}
locations = locations.substring(1, locations.length()-1);
String[] points = locations.split(",");
JSONObject json;
GeoPoint p;
try {
for (int i=0; i<points.length; i++) {
json = new JSONObject(points[i]);
// I guess the "getInt()"s here are the problem
p = new GeoPoint(json.getInt("lat"), json.getInt("lon"));
overlay.addItem(p, "location", "location");
}
} catch (JSONException ex) {
Log.e("debug", ex.getMessage());
}
}
}
我的猜測是,我有把數字放在引號中,但我必須以該格式安全數據(沒有引號中的整數),並且我知道我的數據是有效的JSON。
哪一行會引發錯誤? – stkent 2014-11-01 14:28:17
沒有。我只是在「調試」標籤中得到LogCat中的「Undeterminated object」錯誤消息。該應用程序不會崩潰。 – Quetsalkoatl 2014-11-01 14:37:05