Reputation: 158
I try to get Integers from JSON data. But whenever the method gets called, I get the error:
Undeterminated object at character 16 of {"lat": 47175650
This is my JSON data: [{"lat": 47175650, "lon": 7637853}]
And here's my code to read the data. It's for an Android app an it gets the data from a file, puts all the objects from the JSON array into a string array and should create as many GeoPoints as there are objects:
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());
}
}
}
My guess is that I have to put the numbers in quotes, but I have to safe the data in that format (without the integers in quotes) and I know my data is valid JSON.
Upvotes: 0
Views: 2381
Reputation: 874
You are splitting your JSONObject. Hence [{"lat": 47175650, "lon": 7637853}]
becomes two Strings {"lat": 47175650
and "lon": 7637853}
.
It appears your data is stored as a JSONArray. Hence, replace
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());
}
with
try {
JSONArray array = new JSONArray(locations);
for(int i = 0; i < array.length(); i++) {
JSONObject json = array.getJSONObject(i);
GeoPoint p = new GeoPoint(json.getInt("lat"), json.getInt("lon"));
overlay.addItem(p, "location", "location");
}
} catch (JSONException ex) {
Log.e("debug", ex.getMessage());
}
Upvotes: 1
Reputation: 13007
You should parse the JSON-String as a JSONArray
first.
JSONArray points = new JSONArray(locations);
int length = points.length();
for (int i = 0; i < length; ++i) {
JSONObject point = points.getJSONObject(i);
int lat = point.getInt("lat");
int lon = point.getInt("lon");
}
Upvotes: 0
Reputation: 102
Hace you tried reading it as a string and then cast it to an integer? I mean:
p = new GeoPoint(Integer.valueOf(json.getString("lat")), Integer.valueOf( json.getString("lon")));
Upvotes: 0