2016-07-30 4 views
-2

이 코드는 중복 된 것처럼 보일 수 있지만 다른 답변은 사용하지 않으려 고 시도했습니다. 저는 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.

+1

변경'$의 기타 []'$'에 misc' : 처음에 개체로, 선언 ($misc =하지 $misc[] =) 떨어져 브래킷을 취함으로써. 또한 여러 값을 원한다면 while ('$ misc = array();') 전에'$ misc'을 선언 한 다음에'array_push ($ misc, array ('Id'=> ... = > $ poopR);'배열로 배열을 채울 수 있습니다 (while 루프의 반복 횟수에 대해 잘 모르는 경우). 또한이 작업을 수행하는 동안 while 루프 다음에 '$ misc'을 echo해야합니다. – 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}]

그 대괄호는 배열이 아닌 객체를 보내는 나타냅니다. 당신이 (아마 json_encode($misc[0])을)이 걸릴 수 있습니다, 그래서 당신이 원하는 객체가 아니라 배열의 첫 번째이자 유일한 구성원 다행히

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

또는 $misc 캐스트 : 객체는 (더 대괄호를 알 수 없음)과 같을 것이다

$misc = array('Id'=>$idR,'UserId'=>$user_idR,'Water'=>$waterR,'Poop'=>$poopR);