簡體   English   中英

從Java中的JSON數據獲取整數

[英]getting integer from JSON data in java

我嘗試從JSON數據獲取整數。 但是每當調用該方法時,都會出現錯誤:

Undeterminated object at character 16 of {"lat": 47175650

這是我的JSON數據: [{"lat": 47175650, "lon": 7637853}]

這是我讀取數據的代碼。 它適用於Android應用程序,它從文件獲取數據,將JSON數組中的所有對象放入字符串數組,並應創建與對象一樣多的GeoPoint:

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。

您正在拆分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());
}

您是否曾嘗試將其讀取為字符串,然后將其轉換為整數? 我的意思是:

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

您應該首先將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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM