NSDictionary 中使用“()”格式化的键值
我试图从这本词典中提取两个值,但我得到的值周围有“()”。任何想法是什么导致了这个?
这是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是为了澄清发生了什么事。解析 JSON 时,{} 作为字典返回,而 [] 作为数组返回。所以我们有
useridDict
一个包含解析数据的NSDictionary
。'useridDict' 有一个键
Rows
,它返回一个NSArray
。我们的
useridArray
有一个元素,一个NSDictionary
这个
dict
包含两个键:location
和userid< /代码>
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
anNSDictionary
containing the parsed data.'useridDict' has one key
Rows
which returns anNSArray
.Our
useridArray
has one element, anNSDictionary
This
dict
contains the two keys:location
anduserid
你可以像这样使用。
You can use like this.