これは重複しているように見えるかもしれませんが、他の回答は役に立たないと試しました。私は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':最初の場所でオブジェクトとして、その宣言(
$misc =
、ない$misc[] =
)のオフブラケットを取ることによって。また、複数の値が必要な場合は、while( '$ misc = array();')の前に '$ misc'を宣言し、' array_push($ misc、array( 'Id' => ... = > $ poopR); '配列で配列を埋めることができます(whileループの反復回数についてわからない場合)また、これを行う場合は、whileループの後に' $ misc'をエコーする必要があります。 – ForceMagic