简体   繁体   中英

parsing JSON values from JSON Array in iOS

i am getting the response from the server in JSON array form like below :

{
    "status": "success",
    "data": [
        {
            "auth_Secret_ticket_refresh": "NULL",
            "userId": "10632",
            "loginEmail": "Raushan@gmail.com",
            "auth_authorizationCode": "4cb8c5e8a7f5",
            "accountType": "flickr",
            "auth_Token": "23104658-d2e65d5f94554652",
            "userName": "betterlabpune",
            "auth_subdomain": "NULL"
        },
        {
            "auth_Secret_ticket_refresh": "NULL",
            "userId": "19629",
            "loginEmail": "Ipad@gmail.com",
            "auth_authorizationCode": "8b909cb3e0e1",
            "accountType": "flickr",
            "auth_Token": "77645323118718-bac668bc2b95ad89",
            "userName": "betterlabpune",
            "auth_subdomain": "NULL"
        }
    ]
}

I want to extract the value from JSON array for the 0 index, value for "userId" and "userName". i had tried to extract values in many ways ,below is my code:

 NSMutableData * _responseData = [[NSMutableData alloc]init];
    [_responseData appendData:data];
    NSJSONSerialization *dataAsString=(NSJSONSerialization*)[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    // dataJson=(NSDictionary*)[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"data in register reponse : %@",dataAsString);
    NSJSONSerialization *json;
    NSDictionary *dataJson;
    NSError *error;

    NSDictionary *JSONE = [NSJSONSerialization JSONObjectWithData:_responseData options:0 error:nil];
    NSLog(@"JSONE : %@",JSONE);


    json=[NSJSONSerialization JSONObjectWithData:_responseData
                                         options:NSJSONReadingMutableLeaves
                                           error:&error];

      [self processData:dataJson];
   dataJson=[[NSDictionary alloc]init];
   dataJson=(NSDictionary *)json;
    NSString * status=[dataJson objectForKey:@"status"];
  NSString * message=[[dataJson objectForKey:@"data"]objectForKey:@"id"];

    NSLog(@"status : %@",status);
   NSLog(@"message: %@",message);

    NSMutableArray *argsArray = [[NSMutableArray alloc] init];
    argsArray= [dataJson valueForKeyPath:@"status"];
    NSLog(@" client Id : %@", [argsArray objectAtIndex:0]);

bet every time i get null in my result. Please help me out. Thank you for your precious time.

try this...

if ([[dataJson  valueForKey:@"status"]isEqualToString:@"success"])
    {
        NSMutableArray *tempArray=[NSMutableArray array];
        for (NSDictionary *tempDic in [dataJson valueForKey:@"data"])
        {

            [tempArray addObject:[tempDic valueForKey:@"userId"]];
        }
        NSLog(@"%@",tempArray);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM