2012-02-22 8 views
0

Webサービスを作成する/ iOSにフックする方法については、Ray Wenderlichのチュートリアルに従いましたが、返された結果にはクエリで返される行が1つしかありません。すべての可能な結果を​​JSON形式で返すようにしたいのですが、正しいキーとして格納する方法が混乱しています。ここでiOS用のPHPで複数のクエリをエンコードするJSON

は、私はPHPのために持っているものです:物事のOBJの-C側で

$stmt = $this->db->prepare('SELECT userID, lat, lng FROM Connections WHERE airline=? AND flight_number=? '); 
$stmt->bind_param("si", $airline, $flight_number); 
$stmt->execute(); 
$stmt->bind_result($otherUser, $otherLat, $otherLng); 
    while ($stmt->fetch()) { 
     break; 
    } 
$stmt->close(); 

if ($otherUser) { 
     //sendResponse(403, 'Code already used'); 
     //return false; 
     $myLatLng = "$lat,$long"; 
     $otherLatLng="$otherLat,$otherLng"; 
     $results = getDistanceBetween($myLatLng,$otherLatLng); 
     $miles = $results[0]; 
     $minutes = $results[1]; 
     $result = array(
       "other_user" => $otherUser, 
       "distance" => $miles, 
       "duration" => $minutes, 

       ); 
     sendResponse(200, json_encode($result)); 
     return true; 
    } 

私はこのコードを使用してこれらの値を取得する:

if (request.responseStatusCode == 200) { 
    NSString *responseString = [request responseString]; 
    NSDictionary *responseDict = [responseString JSONValue]; 
    NSString *otherUser = [responseDict objectForKey:@"other_user"]; 
    NSString *otherDistance = [responseDict objectForKey:@"distance"]; 
    NSString *otherDuration = [responseDict objectForKey:@"duration"]; 

誰かが私を助けてくださいことはできますか?

答えて

1

PHPコードでは、ネストされた配列を作成してjson_encode()を使いたいと思っています。ここにあなたの問題のPHP側についての詳細と、前の質問である:iOSの側でHow to create an array for JSON using PHP?

、あなたのWebサービスは、応答文字列にJSONValueを呼び出して、複数のアイテムを表すJSONレスポンスを返しますので、代わりにNSArrayオブジェクトを返します。 NSDictionaryの次に、配列内の項目を繰り返し処理し(NSDictionary項目)、必要な値を取り出します。

NSString *responseString = [request responseString]; 
NSArray *responseArray = [responseString JSONValue]; 

for (NSDictionary* item in responseArray) { 
    NSString *otherUser = [item objectForKey:@"other_user"]; 
    NSString *otherDistance = [item objectForKey:@"distance"]; 
    NSString *otherDuration = [item objectForKey:@"duration"]; 
} 
+0

ありがとうございますが、私はJSON形式で配列を返すことができますか?私はPHP/SQL/JSONの部分と混同していますが、iOSの配列部分については考慮していませんでした – mkral

+0

PHPについての一番上の部分は表示されませんでした。ありがとうございました! – mkral

関連する問題