开发者

key values from an NSDictionary formatting with "()"

开发者 https://www.devze.com 2023-01-27 16:56 出处:网络
I\'m trying to pull two values from this Dictionary, But the values I\'m getting have \"()\" around them. Any Ideas what is causing this?

I'm trying to pull two values from this Dictionary, But the values I'm getting have "()" around them. Any Ideas what is causing this?

Here is the ServerOutput:

{"Rows":[{"userid":"1","location":"beach"}]}

Dictionary after JSON:

 {
    Rows =     (
            {
        location = beach;
        userid = 1;
    }
);
}

This is what I'm getting:

location : (
    beach
  )
user Id : (
    1
  )

Both the userid and the location key values have the "()". Here is the code. Thanks a lot.

    NSString *serverOutput= [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];


if(serverOutput > 1){

       SBJSON *jsonFF = [[SBJSON new] autorelease];
       NSError *erro开发者_JS百科r3 = nil;
       NSDictionary *useridDict= [jsonFF objectWithString:serverOutput error:&error3];

       NSLog(@"useridDict: %@",useridDict);

    idreturn = [[useridDict valueForKey:@"Rows"] valueForKey:@"userid"];

    locationreturn = [[useridDict valueForKey:@"Rows"] valueForKey:@"location"];

    NSLog(@" user Id : %@", idreturn);
    NSLog(@" location : %@", locationreturn);


Just to clarify what is going on. When parsing JSON {} gets returned as a dictionary and [] gets retured as an array. So we have useridDict an NSDictionary containing the parsed data.

'useridDict' has one key Rows which returns an NSArray.

NSArray *useridArray = [useridDict objectForKey:@"Rows"];

Our useridArray has one element, an NSDictionary

NSDictionary *dict = [useridArray objectAtIndex:0];

This dict contains the two keys: location and userid

NSString *location = [dict objectForKey:@"location"];
NSInteger userid = [[dict objectForKey:@"userid"] intValue];


You can use like this.

idreturn = [[[useridDict valueForKey:@"Rows"] objectAtIndex:0]valueForKey:@"userid"];

locationreturn = [[[useridDict valueForKey:@"Rows"] objectAtIndex:0] valueForKey:@"location"];
0

精彩评论

暂无评论...
验证码 换一张
取 消