2013-05-20 77 views
0

迴應:「用戶名或密碼無效」如何獲取JSON值?

{ 
     "AT": null, 
     "EMs": [ 
      { 
       "EC": null, 
       "LM": null, 
       "SCs": 0, 
       "SM": "Username or Password is invalid." 
      } 
     ], 
     "OS": 1, 
     "PWD": "3456", 
     "UId": 399, 
     "UN": "bb", 
     "COM": "Apple", 
     "DId": 0, 
     "DN": "iPhone", 
     "DOS": "iOS", 
     "EA": "[email protected]", 
     "FN": "bb", 
     "IsCon": true, 
     "IsSuc": false, 
     "SD": "bb", 
     "SLT": "XU0QpDVC", 
     "STs": 0, 
     "UQId": "1d3346c", 
     "US": 2, 
     "Ver": "6.0" 
    } 

這裏我要像獲取SM標記值

我已經試過這個代碼

NSError *error; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 

NSString *SMString = [json objectForKey:@"SM"]; 
NSString *OSString = [json objectForKey:@"OS"]; 
NSString *DIdString = [json objectForKey:@"DId"]; 
NSString *UIdString = [json objectForKey:@"UId"]; 
+0

它在Phonegap代碼中。 –

+0

json(NSDictionary)的響應是什麼? –

+0

請詳細說明你的問題後,我們將能夠幫助你 – iEinstein

回答

2

我希望這個工作:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData 
                options:kNilOptions 
                 error:&error]; 
NSArray *EMsArray = [json objectForKey:@"EMs"]; 
NSAssert([EMsArray count] > 0, @"Expected at least one element in EMs array"); 
NSString *SMString = [[EMsArray objectAtIndex:0] objectForKey:@"SM"]; 
0

嘗試這樣,

NSArray *EMsArray = [json objectForKey:@"EMs"]; 
    if([EMsArray count]>0) 
     NSString *SMString = [[EMsArray objectAtIndex:0] objectForKey:@"SM"]; 
1

你感興趣的是數據字典內的數組,因此您需要執行以下操作:

NSArray *EMsArray = [json objectForKey:@"EMs"]; 

for (NSDictionary *EMsDictionary in EMsArray) { 
    NSString *SMString = [EMsArray objectForKey:@"SM"]; 

    // do something with SMString 
} 
0

試試這個:

NSError *error; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 

NSArray *EMs = [json objectForKey:@"EMs"]; 
NSDictionary *dict = [EMS objectAtIndex:0]; 
NSString *str = [dict objectForKey:@"SM"]; 

NSLog(@"%@",str); 
0

試試這個代碼:

NSArray *EMsArray = [json objectForKey:@"EMs"]; 

for (int i=0;i<[EMsArray count];i++) { 

NSDictionary *dicObj = [EMsArray objectAtIndex:i] 
NSString *str = [dicObj objectForKey:@"SM"]; 

NSLog(@"JSON value is:%@",str); 

} 

希望這有助於!

0

正如我可以在你的json看到它包含一個標籤爲「EMs」的數組。

因此,您首先需要從json中提取數組,並且需要獲取該數組的inde 0字典。 然後你可以簡單的使用

objectForkey 

的NSDictionary的財產和獲取信息你想。

SBJSON *json = [SBJSON new]; 

NSError *error = [[NSError alloc] init]; 

NSString *jsonString = [json objectWithString:"your json string" error:&error]; 

NSArray *array = [[NSArray alloc]initWithArray:[jsonString valueForKeyPath:@"EMs"]]; 

if([array count]>0) 
{ 
    NSDictionary *_dic = [array objectAtIndex:0]; 
    NSLog(@"%@",[_dic objectForKey:@"SM"]); 
} 

參閱https://github.com/fbsamples/ios-friend-smash/tree/master/friendsmash_complete/friendSmasher/Classes爲JSON庫

0

最容易和優雅的解決方案是使用的keyPath這樣的:

NSString *SMString = [json valueForKeyPath:@"EMs.SM"]; 

參見示例代碼:

NSString *SMString = [dict valueForKeyPath:@"EMs.SM"]; 
NSLog(@"Value of key EMs -> SM: %@", SMString); 

,其輸出:

Value of key EMs -> SM: (
    "Username or Password is invalid." 
) 
0

下面的代碼可以幫助你

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 

NSMutableArray *EmsArray=[json objectForKey:@"EMs"]; 
NSMutableDictionary *dataDic=[EMsArray objectAtIndex:0]; 
NSString *smString=[dataDic objectForKey:@"SM"]; 

我認爲在你的迴應陣列每次你只能得到一個對象。