从java中获取JSON数据的整数

时间:2014-11-01 14:18:35

标签: java android json

我尝试从JSON数据中获取Integers。但每当调用该方法时,我都会收到错误:

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。

3 个答案:

答案 0 :(得分:1)

您正在拆分JSONObject。因此,[{"lat": 47175650, "lon": 7637853}]变为两个字符串{"lat": 47175650"lon": 7637853}

您的数据似乎存储为JSONArray。因此,替换

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

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

答案 1 :(得分:0)

Hace你试过把它作为一个字符串读出然后把它变成一个整数?我的意思是:

p = new GeoPoint(Integer.valueOf(json.getString("lat")), Integer.valueOf( json.getString("lon"))); 

答案 2 :(得分:0)

您应首先将JSON-String解析为JSONArray

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