2014-11-01 75 views
0

我嘗試從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。

+0

哪一行會引發錯誤? – stkent 2014-11-01 14:28:17

+0

沒有。我只是在「調試」標籤中得到LogCat中的「Undeterminated object」錯誤消息。該應用程序不會崩潰。 – Quetsalkoatl 2014-11-01 14:37:05

回答

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

聖牛,你的對,我把它搞砸了split()。我怎麼錯過了!我不知道JSONArray ... – Quetsalkoatl 2014-11-01 14:52:32

+0

對於我來說第四行需要是「JSONObject obj = array.getJSONObject(i);」 – Quetsalkoatl 2014-11-01 14:54:14

+0

還有兩件事:JSONArray需要設置在try/catch中,並且有太多的東西。否則,謝謝,它的工作! (: – Quetsalkoatl 2014-11-01 15:14:00

0

Hace你試着把它看作一個字符串,然後將它轉換爲一個整數?我的意思是:

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

現在我有。還是一樣的信息... – Quetsalkoatl 2014-11-01 14:41:03

0

你應該解析JSON字符串作爲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"); 
}