0

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.

2
  • Which line throws the error? Commented Nov 1, 2014 at 14:28
  • None really. I just get the "Undeterminated object" error message in LogCat with a "debug" tag. The app doesn't crash. Commented Nov 1, 2014 at 14:37

3 Answers 3

1

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());
}
Sign up to request clarification or add additional context in comments.

3 Comments

Holy cow, your're right i messed it up with split(). How did I miss that! And I didn't know about JSONArray...
For me the 4th line needs to be "JSONObject obj = array.getJSONObject(i);"
Two more things: the JSONArray needs to be set inside of the try/catch and there is one } too much. Otherwise THANKS, it worked! (:
0

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"))); 

Comments

0

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.