2017-08-18 37 views
0

下面是我試圖做的事情。有兩個數組我想從我的mysql數據庫中檢索。這兩個陣列被分別呼應:JSONObject中的第二個JSONArray莫名其妙地是空的

echo json_encode(array("friendrequest"=>$friendrequest)); 
echo json_encode(array("friendresult"=>$friendresult)); 

這是我使用來處理所得到的字符串的代碼:

public void onResponse(String response) { 
     Log.d("Response", response); 
     try { 

      JSONObject jsonObj = new JSONObject(response); 

      JSONArray ja_data = jsonObj.getJSONArray("friendresult"); 
      String[] from = { 
       "first_name", 
       "anytimer_count", 
       "relationship_status" 
      }; //string array 
      int[] to = { 
       R.id.textListView1, 
       R.id.textListView2, 
       R.id.textListView3 
      }; //int array of views id's 
      JSONArrayAdapter arrayListAdapter = new JSONArrayAdapter(MyFriendsActivity.this, ja_data, R.layout.custom_list_items, from, to); 
      list.setAdapter(arrayListAdapter); 


      JSONArray ja_requests = jsonObj.getJSONArray("friendrequest"); 
      int ja_lengths = ja_requests.length(); 
      Log.d("Response", Integer.toString(ja_lengths)); 

     } catch (JSONException e) { 
      Log.e("MyFriendsActivity", "unexpected JSON exception", e); 
     } 

現在,從在處理代碼線2所記錄的效應初探給我以下內容:

響應=

{ 
    "friendrequest": [ 
    { 
     "first_name": "Tom", 
     "anytimer_count": "0", 
     "relationship_status": "0" 
    }, 
    { 
     "first_name": "Bert", 
     "anytimer_count": "0", 
     "relationship_status": "0" 
    } 
    ] 
}{ 
    "friendresult": [ 
    { 
     "first_name": "Luuk", 
     "anytimer_count": "0", 
     "relationship_status": "1" 
    } 
    ] 
} 

這是包含的是,如果所有的值被髮送並且是正確的。

問題是我的處理代碼只能識別首先在php腳本中編碼的數組。基本上,如果我在PHP腳本中交換兩行代碼的位置,'friendrequest'將包含值,而'friendresult'不會,反之亦然。我在這裏做錯了什麼?

錯誤即時得到:

org.json.JSONException:爲friendresult

No值在 com.example.tomva.anytimereverywhere.MyFriendsActivity $ 3.onResponse(MyFriendsActivity.java:84)

線84下面的行:

JSONArray ja_data = jsonObj.getJSONArray("friendresult"); 
+2

如果這是您的要求充分反應,它不是一個有效的JSON。在兩個對象之間缺少[']'和','。比你的要求是給你一個JsonArray與2對象,..... – Fusselchen

回答

2

你必須通過你的數組中的json才能夠將它們全部提取出來。

應和:

[ 
<?php 
    echo json_encode(array("friendrequest"=>$friendrequest)); 
    echo ","; 
    echo json_encode(array("friendresult"=>$friendresult)); 
?> 
] 

Related answer

,或者您可以使用以下

echo json_encode(array("friendrequest"=>$friendrequest,"friendresult"=>$friendresult)); 
+0

是的,這將產生一個有效的JsonArray。 @ sam-chaudhari的答案更好。現在,您必須將您的代碼從解析轉換爲JsonObject t JsonArray,其中包含2個對象,然後您可以使用您的代碼。 – Fusselchen

+0

謝謝@Fusselchen –

1

更換

echo json_encode(array("friendrequest"=>$friendrequest)); 
echo json_encode(array("friendresult"=>$friendresult)); 

有了這個

echo json_encode(array("friendrequest"=>$friendrequest,"friendresult"=>$friendresult)); 
+0

這樣做的好處,非常感謝! –

+0

歡迎@TomvanNes –

0

您迴應JSON是無效,因爲它已經 「丟失」, 嘗試你的反應

{"friendrequest": [{"first_name":"Tom","anytimer_count":"0","relationship_status":"0"},{"first_name":"Bert","anytimer_count":"0","relationship_status":"0"}]} {"friendresult": [{"first_name":"Luuk","anytimer_count":"0","relationship_status":"1"}]} 

更改爲

[{ 
    "friendrequest": [{ 
     "first_name": "Tom", 
     "anytimer_count": "0", 
     "relationship_status": "0" 
    }, { 
     "first_name": "Bert", 
     "anytimer_count": "0", 
     "relationship_status": "0" 
    }] 
}, { 
    "friendresult": [{ 
     "first_name": "Luuk", 
     "anytimer_count": "0", 
     "relationship_status": "1" 
    }] 
}] 

常見錯誤

  • 期待「STRING」 - 你可能有一個額外的逗號在
    您的收藏結束。例如{「a」:「b」,}
  • 期待'STRING','NUMBER','NULL','TRUE','FALSE','{','[' - 您可能有列表末尾有額外的逗號。 類似於:[「a」,「b」,]

  • 將引用中的集合鍵括起來。
    集合的適當格式爲{「key」:「value」}

  • 確保遵循JSON的語法。例如,總是 使用雙引號,總是quotify你的鑰匙,並刪除所有 回調函數