如何从 iOS 中的字典中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19970064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get values from a dictionary in iOS
提问by Saba Sayed
I am new to iOS. I created a login page and everything works fine. I used JSONfor checking username and password and got the response from server in a dictionaryformat. I want to extract the values from the dictionaryand check them in my program. The response which I get from the server is:
我是iOS新手。我创建了一个登录页面,一切正常。我使用JSON来检查用户名和密码,并以字典格式从服务器获得响应。我想从字典中提取值并在我的程序中检查它们。我从服务器得到的响应是:
json: {
error = 0;
msg = "";
value = {
user = false;
};
};
First I want to check if the value with the key error
is 0
or 1
. Then I want to check the value with the key user
. I don't know how I should code to check it. Can anyone help?
首先,我想检查带键的值error
是否为0
or 1
。然后我想用 key 检查值user
。我不知道我应该如何编码来检查它。任何人都可以帮忙吗?
The code which I tried is below:
我试过的代码如下:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *respString = [[NSString alloc] initWithData:loginJSONData encoding:NSUTF8StringEncoding];
SBJsonParser *objSBJSONParser = [[SBJsonParser alloc] init];
NSDictionary *json = [[NSDictionary alloc] initWithDictionary:[objSBJsonParser objectWithString:respString]];
NSLog(@"json: %@",json);
NSString *error = [json objectForKey:@"error"];
NSLog(@"error: %@", error);
if ([error isEqualToString:@"o"])
{
NSLog(@"login successful");
}
else
{
NSLog(@"login fail");
}
}
回答by cream-corn
Using modern Objective-C, accessing things in arrays and dictionaries become easier.
使用现代 Objective-C,访问数组和字典中的内容变得更加容易。
You should use the following syntax:
您应该使用以下语法:
id<NSObject> value = dictionary[@"key"];
id<NSObject> value = dictionary[@"key"];
Similarly,
相似地,
id<NSObject> value = array[1]; // 1 is the index
id<NSObject> value = array[1]; // 1 is the index
Applying the above to the question:
将上述应用于问题:
NSString *error = json[@"error"];
NSString *error = json[@"error"];
NSDictionary *value = json[@"value"];
NSDictionary *value = json[@"value"];
BOOL user = [json[@"value"][@"user"] boolValue];
BOOL user = [json[@"value"][@"user"] boolValue];
As in the above line, nesting is allowed, but it is not a good practice.
在上面的行中,允许嵌套,但这不是一个好习惯。
回答by Inder Kumar Rathore
NSNumber *error = [json objectForKey:@"error"];
if ([error intValue] == 0)
{
NSLog(@"login successful");
NSDictionary *value = [json objectForKey:@"value"];
NSNumber *user = [value objectForKey:@"user"];
if ([user boolValue])
{
NSLog(@"user == true");
}
else
{
NSLog(@"user == false");
}
}
else
{
NSLog(@"login failed");
}
回答by SARATH SASI
The dictionaries that normally returning from the server will be as a key value pair. if you are looking for accessing these values that corresponds to a key, then these code may help you
通常从服务器返回的字典将作为键值对。如果您正在寻找访问与键对应的这些值,那么这些代码可能对您有所帮助
NSString *varname = [[NSString alloc]initWithFormat:@"%@",[dictionaryname objectForKey:@"key"]];
回答by iPatel
For get errorvalue from your JSON
Dictionary.
从您的字典中获取错误值JSON
。
NSString *error = [myJSONDicName objectForKey:@"error"];
For get uservalue from your JSON
Dictionary.
从您的字典中获取用户价值JSON
。
NSString *error = [[myJSONDicName objectForKey:@"value"] objectForKey:@"user"];
EDITED:
编辑:
You just need to change In your
你只需要改变你的
if ([error isEqualToString:@"o"])
_^_
|
change 'o'
to '0'
更改'o'
为'0'
回答by manujmv
You can use the below code
您可以使用以下代码
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *respstring = [[NSString alloc]initWithData:loginJsonData encoding:NSUTF8StringEncoding];
NSDictionary *dic = [responseString JSONValue];
NSLog(@"%@",dic);
NSNumber *error = [dic objectForKey:@"error"];
if([error intValue] == 0) {
NSLog(@"login successful");
}
else
{
NSLog(@"login fail");
}
NSString *user = [value objectForKey:@"user"];
BOOL userStatus = [user boolValue];
}