2016-07-30 7 views
-2

これは重複しているように見えるかもしれませんが、他の回答は役に立たないと試しました。私は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.

+1

変更 '$雑貨[]' '$へmisc':最初の場所でオブジェクトとして、その宣言($misc =、ない$misc[] =)のオフブラケットを取ることによって。また、複数の値が必要な場合は、while( '$ misc = array();')の前に '$ misc'を宣言し、' array_push($ misc、array( 'Id' => ... = > $ poopR); '配列で配列を埋めることができます(whileループの反復回数についてわからない場合)また、これを行う場合は、whileループの後に' $ misc'をエコーする必要があります。 – ForceMagic

答えて

-1

問題は、あなたの配列が正しくフォーマットされていなかったということです。:型はJSONオブジェクト(例えば{「値」「名」})が必要:

$misc = ['Id'=>$idR,'UserId'=>$user_idR,'Water'=>$waterR,'Poop'=>$poopR]; 
echo json_encode($misc); 
0

エラーメッセージは、必要なすべての情報を持っているようです。あなたはJSON 配列を、JSONオブジェクトが必要な関数に渡そうとしています。

php returns [{"Id":25,"UserId":24,"Water":0,"Poop":1}]

これらの角かっこは、オブジェクトではなく配列を送信していることを示しています。オブジェクトは、(無角括弧に気づかない)、次のようになります。

{"Id":25,"UserId":24,"Water":0,"Poop":1} 

あなたがそれを取る(おそらくjson_encode($misc[0]))または$miscをキャストすることができますいずれかのように、幸いにも、あなたがしたいオブジェクトは、また、配列の最初で唯一のメンバーです

$misc = array('Id'=>$idR,'UserId'=>$user_idR,'Water'=>$waterR,'Poop'=>$poopR); 
関連する問題