2016-09-14 45 views
-1

我想用AFNetworking庫代碼如何解析JSON在Objective-C的iOS

{ 
    "success": 1, 
    "row": [ 
    { 
     "amount": 2800 
    } 
    ] 
} 

iOS中解析JSON在Objective-C -響應

arrAmount = [responseObject valueForKey:@"row"]; 
NSLog(@"%@",arrAmount); 

NSString *strAmount =[NSString stringWithFormat:@"%@", [arrAmount valueForKey:@"amount"]]; 

self.lblAmount.text =[NSString stringWithFormat:@"%@",strAmount]; 

回答

2

row包含數組不Dictionary,因此從Array的第一個對象訪問數量。

NSarray *arrAmount = [responseObject objectForKey:@"row"]; 
if arrAmount.count > 0 { 
    NSDictionary *dic = [arrAmount objectAtIndex:0]; 
    NSString *strAmount = [NSString stringWithFormat:@"%d", [dic objectForKey:@"amount"]]; 
    self.lblAmount.text = strAmount; 
} 
1

使用的NSDictionary:

NSDictionary *dict = [responseObject valueForKey:@"row"]; 
NSLog(@"value = %@",[dict valueForKey:@"amount"]); // fetch amount value 

array = [dict valueForKey:@"amount"]; // // fetch all amount value in array 
NSLog(@"array = %@", array.description); 
+0

這不會起作用,因爲關鍵'row'包含數組沒有字典。 –

+0

@NiravD很高興有你的關注,但這將工作:)) – vaibhav

+0

你能解釋一下嗎? –

0

使用objectForKey而不是valueForKey

NSDictionary *responseObject; 
NSArray *arrAmount; 
arrAmount = [responseObject objectForKey:@"row"]; 
NSLog(@"%@",arrAmount); 
NSString *strAmount =[NSString stringWithFormat:@"%@", [[arrAmount objectAtIndex:0] objectForKey:@"amount"]];