Hai我需要获得id&登录服务的状态我的代码如下。请指导我获取价值..提前致谢..
NSString *Username= txtUsername.text;
NSString *Password=txtPassword.text;
NSString *link = [NSString stringWithFormat:@"http://www.xxx/login.php?user=%@&pass=%@&format=json",Username,Password];
NSURL *url=[NSURL URLWithString:link];
NSData *data=[NSData dataWithContentsOfURL:url];
答案 0 :(得分:1)
(无效)viewDidLoad中 {
[super viewDidLoad];
NSString * Username = txtUsername.text;
NSString * Password = txtPassword.text;
NSString * link = [NSString stringWithFormat:@“http://www.some.com/webservice/login.php?user=%@&pass=%@&format=json”,用户名,密码];
dispatch_async(kBgQueue,^ {
NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL]; [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
}); }
然后调用该选择器fetchedData
(void)fetchedData:(NSData *)responseData {
//解析出json数据
NSError *错误;
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:responseData
选项:kNilOptions 误差:&安培;错误];如果(!错误){
NSArray * postArray = [json objectForKey:@“posts”]; //这是一个数组
if(postArray.count> 0){
NSDictionary *dict = [[postArray objectAtIndex:0] objectForKey:@"post" ]; NSString *id_ = [dict objectForKey:@"id"]; NSString *status_ = [dict objectForKey:@"status"];
} } }
答案 1 :(得分:0)
你可以发布你的json字符串吗?您可以使用NSJSONSERIALISATION将数据(json字符串)转换为NSDictionary。然后使用键提取值。我通过手机回复,所以我无法写出实际的代码。
答案 2 :(得分:-2)
使用下面的代码解析IOS中的Json
NSString *Username= txtUsername.text;
NSString *Password=txtPassword.text;
NSString *link = [NSString stringWithFormat:@"http://www.some.com/_webservice/login.php?user=%@&pass=%@&format=json",Username,Password];
NSURL *url=[NSURL URLWithString:link];
NSMutableURLRequest *req1 = [NSMutableURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error;
//getting the data
NSData *newData = [NSURLConnection sendSynchronousRequest:req1 returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];
NSLog(@"basavaraj \n\n\n %@ \n\n\n",responseString);
NSData* data = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
NSString *id=[res objectForKey:@"ID"];
NSString *status=[res objectForKey:@"Status"];
如果你需要额外的信息,请通过以下链接,它可以帮助你