2015-04-07 50 views
0

這是我查詢的JSON結果。使用子數組解析數據android時出錯

{ 
    "_id":{"$id":"551fb585ecba12c819000032"}, 
    "nome":"Google","loc":[-122.083983,37.422969], 
    "icona":1, 
    "istituzione_id":{"$id":"551fb556ecba12c819000031"} 
    } 
    { 
    "_id":{"$id":"5520fe2becba12c003000029"}, 
    "nome":"Oracle","loc":[-122.262168,37.531595], 
    "icona":1, 
    "istituzione_id":{"$id":"551fb556ecba12c819000031"} 
    } 

我試圖解析JSON結果以這種方式:

try { 
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 

      while ((line = reader.readLine()) != null) { 
        Log.e("log_a_line", line); 
       sb.append(line + "\n"); 
      } 

      is.close(); 
      result = sb.toString(); 
        Log.e("log_a_result", result); 

     } catch (Exception e) 
       { 
        Log.e("log_tag_convert", "Error converting result" + e.toString()); 
       } 

     //-----PARSER---- 
     try { 
      JSONArray jArray = new JSONArray(result); 
      for (int i = -1; i < jArray.length() - 1; i++) { 
       JSONObject json_data = jArray.getJSONObject(i); 
       Log.i("log_tag", "id:" + json_data.getString("_id")); 
      } 
     } 
     catch (JSONException e) 
      { 
      Log.e("log_tag_parsing", "Error parsing data" + e.toString()); 
      } 

但運行應用程序後,在我的logcat我發現錯誤:

log_tag_parsing﹕ Error parsing dataorg.json.JSONException: Value {"icona":1,"loc":[-122.083983,37.422969],"nome":"Google","istituzione_id":{"$id":"551fb556ecba12c819000031"},"_id":{"$id":"551fb585ecba12c819000032"}} of type org.json.JSONObject cannot be converted to JSONArray 

我還以爲問題是關於子數組loc:[....]也許。

我無法達成解決方案。有人可以幫助我嗎?

回答

0

org.json.JSONObject cannot be converted to JSONArray

答案出現在你的例外中。您正試圖將jsonobject轉換爲jsonArray。 將結果轉換爲JsonObject

+0

確定這回只是第一個項目... – Motosega

0

在這裏看到您正在獲取JSONObject。但在解析時,您期待JSONArray。

解析之前首先添加一個檢查結果。

E.g.

if (resultObj instanceof JSONArray) { 
    // process Array 
} 
else if (resultObj instanceof JSONObject) { 
    // processObj 
} 
1

您在對象之間缺少逗號;對於陣列使用[],對象使用{}

下一次使用jsonlint.com先驗證您的JSON。

0

JSON數組語法應該是這樣的一個

員工是陣列名稱

[] //爲包含一個對象和另一個對象之間{}陣列,

"employees":[ 
    {"firstName":"John", "lastName":"Doe"}, 
    {"firstName":"Anna", "lastName":"Smith"}, 
    {"firstName":"Peter","lastName":"Jones"} 
] 

創建正確json array

編輯

生成JSON

$a = array(
     array('foo' => 'bar'), 
     array('foo' => 'baz')); 
$json = json_encode($a); 

的數組給定

[{"foo":"bar"},{"foo":"baz"}] 

更多

http://php.net/manual/en/function.json-encode.php

+0

json是在我的php文件中生成的「echo json_encode($ row);」 – Motosega

+0

make array然後編碼它echo json_encode($ arr);其中ARR是你的陣列 –

0

爲什麼你的for循環已經開始值爲-1和更改日誌打印線。請參見下面的代碼:

//-----PARSER---- 
     try { 
      JSONArray jArray = new JSONArray(result); 
      for (int i = -1; i < jArray.length() - 1; i++) { //this line 
       JSONObject json_data = jArray.getJSONObject(i); 
       Log.i("log_tag", "id:" + json_data.getString("_id"));//this line 
      } 
     } 

變化都提到線爲:

//-----PARSER---- 
      try { 
       JSONArray jArray = new JSONArray(result); 
       for (int i = 0; i < jArray.length(); i++) { //this line 
        JSONObject json_data = jArray.getJSONObject(i); 
        Log.i("log_tag", "id:" + json_data.getString("$id")); 
       } 
      } 
+0

不工作... – Motosega

+0

你是否得到相同的錯誤? – Pankaj

+0

是的,同樣的錯誤。 – Motosega