這看起來像是重複的,但我試過其他答案無濟於事。我使用Xamarin和PHP。我的PHP查詢有效,但它沒有正確返回格式化的PHP。我使用PHP 5.6並檢查JSON。我的PHP是:PHP沒有編碼到JSON
$stmt=$conn->prepare("SELECT id,user,water,poop FROM dailyMisc WHERE user = ? AND misc_day = ?");
$stmt->bind_param('is',$userId,$dateSelected);
$stmt->execute();
$stmt->bind_result($idR,$user_idR,$waterR,$poopR);
$stmt->store_result();
while($stmt->fetch())
{
$misc[] = array('Id'=>$idR,'UserId'=>$user_idR,'Water'=>$waterR,'Poop'=>$poopR);
echo json_encode($misc);
}
PHP回報:
[{"Id":25,"UserId":24,"Water":0,"Poop":1}]
我Xamarin錯誤:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'BitesBoardMobile.businessObject.BusinessMisc' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
變化'$雜項[]''到$ misc'。此外,如果你想要多個值,那麼我建議在while('$ misc = array();')之前聲明'$ misc',然後使用'array_push($ misc,array('Id'=> ... = > $ poopR);''你可以用數組填充數組(如果你不確定while循環的迭代次數)。另外,如果你這樣做,你必須在while循環之後echo'$ misc'。 – ForceMagic