2016-12-12 4 views
0

私はこの使用している: -辞書レスポンスエラー

NSString *srtUrl = [NSString stringWithFormat:@"http://api.ptccircle.co.in/RestServiceImpl.svc/compose/0/iOS/iOS%20app/SRORTK5248/SRORTK/0/new/12-12-2016"]; 

NSDictionary *dicResult = [WebServiceHelper GetDataFromServer:srtUrl]; 

をそして私が手に持っている応答: -

私は条件
if([[dicResult valueForKey:@"Status"] isEqual: @"1"]) 
{ 
NSLog(@"Success"); 
} 
else 
{ 
NSLog(@"Unsuccess"); 
} 

私の問題があることである

にチェックしたい
(
    { 
     Status = 1; 
    } 
) 

、私はUnsuccess出力を持っています。 誰でも私を助けることができます...

+0

で、ステータスが文字列か数字かをチェックしますか?上記のコードで文字列をチェックしています。 – PiyushRathi

答えて

2

あなたの応答はまたあなたのステータスキーが文字列のようにこの方法を使用して、それをチェックしませブール値が含まれ、直接の辞書ではない辞書の配列が含まれています。

NSArray *array = [WebServiceHelper GetDataFromServer:srtUrl]; 
NSDictionary *dicResult = [array firstObject]; 
NSNumber *status = [dicResult objectForKey:@"Status"] 
if ([status boolValue] == YES) { 
    NSLog(@"Success"); 
} 
else { 
    NSLog(@"Unsuccess"); 
} 
+0

私はNSDictionaryで応答しています.. NSDictionary * dicResult = [WebServiceHelper GetDataFromServer:srtUrl]; Arrayでレスポンスを変更することはできません。サードパーティを使用しており、辞書形式のレスポンスを得ることができます。 –

+0

NSDictionaryを使用して他の方法を提供してください... –

+0

@HariMohan配列を取得しているので、また、 'dicResult'をnslogしてコンソールに何が印刷されているかを確認してください。 –

1

以下のように結果を確認する必要があります。応答ステータスのタイプがブール値の場合は です。あなたは配列から最初のオブジェクトにアクセスする必要があるので、

if ([[dicResult objectForKey:@"Status"]boolValue]) { 
    NSLog(@"Success"); 
} 
0

ソリューションを取得するには、以下のようにあなたのGetDataFromServer方法を変更します。 このメソッドでは、サーバーから取得したデータを返します。それは辞書か配列かもしれないので私達はちょうどidオブジェクトを戻します。

+(id)GetDataFromServer:(NSString *)strUrl 
{ 

    NSString *urlString = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSURL *myURL = [NSURL URLWithString:urlString]; 
    NSData *myData = [[NSData alloc] initWithContentsOfURL:myURL]; 
    NSError *err = nil; 
    id json = [NSJSONSerialization JSONObjectWithData:myData options:kNilOptions error:&err]; 
    return json; 

} 

我々は上記の方法だけで、我々は、オブジェクトの種類を、我々は応答として得たことを確認する必要がありますので、URLを呼び出し、応答を返すことを知っているよう。

NSString *strURL = [NSString stringWithFormat:@"http://api.ptccircle.co.in/RestServiceImpl.svc/compose/0/iOS/iOS%20app/SRORTK5248/SRORTK/0/new/12-12-2016"]; 

//Calling Method 
id response =[WebServiceHelper GetDataFromServer:strURL]; 

//checking that this is 
if (response != nil && [response isKindOfClass:[NSArray class]]) { 
    //Cast your response to array 
    NSArray *responseArray= response; 

    if (responseArray.count > 0) 
    { 

     if ([[[responseArray firstObject] objectForKey:@"Status"] boolValue]) 
     { 
      NSLog(@"True"); 
     } 
     else 
     { 
      NSLog(@"False"); 
     } 
    } 
} 
else 
{ 
    //check its dictionary or nil 
}