이 코드는 중복 된 것처럼 보일 수 있지만 다른 답변은 사용하지 않으려 고 시도했습니다. 저는 Xamarin과 PHP를 사용하고 있습니다. 내 PHP 쿼리가 작동하지만 제대로 formated 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}]
내 자 마린 오류 :
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'을 echo해야합니다. – ForceMagic