NSDictionary 中使用“()”格式化的键值

发布于 2024-10-05 06:56:12 字数 1033 浏览 0 评论 0原文

我试图从这本词典中提取两个值,但我得到的值周围有“()”。任何想法是什么导致了这个?

这是 ServerOutput:

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

JSON 之后的字典:

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

这就是我得到的:

location : (
    beach
  )
user Id : (
    1
  )

userid 和 location 键值都有“()”。这是代码。多谢。

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


if(serverOutput > 1){

       SBJSON *jsonFF = [[SBJSON new] autorelease];
       NSError *error3 = 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);

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 *error3 = 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救赎№ 2024-10-12 06:56:12

只是为了澄清发生了什么事。解析 JSON 时,{} 作为字典返回,而 [] 作为数组返回。所以我们有 useridDict 一个包含解析数据的 NSDictionary

'useridDict' 有一个键 Rows,它返回一个 NSArray

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

我们的 useridArray 有一个元素,一个 NSDictionary

NSDictionary *dict = [useridArray objectAtIndex:0];

这个 dict 包含两个键:locationuserid< /代码>

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

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];
长途伴 2024-10-12 06:56:12

你可以像这样使用。

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

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

You can use like this.

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

locationreturn = [[[useridDict valueForKey:@"Rows"] objectAtIndex:0] valueForKey:@"location"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文