将Json转换为Ios格式
我尝试在iOS中解析json,但出现了错误。 这是我从api获得的json将Json转换为Ios格式,ios,json,parsing,jsonp,Ios,Json,Parsing,Jsonp,我尝试在iOS中解析json,但出现了错误。 这是我从api获得的json data = ( { LogIn = { Emails = "abc@xyz`.com"; Latitude = "37.33233141"; Longitude = "-122.03121860"; Password = 123456; }; Status = Success; } ); 我想要的所有细节,如电子邮件,纬度,经度和密码,以及状态。我正在尝试下面的json代码 login
data = (
{
LogIn = {
Emails = "abc@xyz`.com";
Latitude = "37.33233141";
Longitude = "-122.03121860";
Password = 123456;
};
Status = Success;
}
);
我想要的所有细节,如电子邮件,纬度,经度和密码,以及状态。我正在尝试下面的json代码
loginurl= [NSURL URLWithString:string1];
NSLog(@"url is %@",string1);
dispatch_async(kBgQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:loginurl];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
这是我的获取数据方法-
-(void)fetchedData:(NSData *)responseData
{
NSError * error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"JSON: %@", json);
NSDictionary *data = [json objectForKey:@"data"];
NSArray *currentConditions = [data objectForKey:@"LogIn"];
NSDictionary *conditions = [currentConditions objectAtIndex:0];
NSLog(@"%@",conditions);
}
首先,您的JSON格式无效。应该是这样的:
{
"data": {
"LogIn": {
"Emails": "abc@xyz`.com",
"Latitude": "37.33233141",
"Longitude": "-122.03121860",
"Password": 123456
},
"Status": "Success"
}
}
您可以使用验证JSON数据
第二,
[data objectForKey:@"LogIn"];
是一个字典,而不是数组
尝试:
NSDictionary *currentConditions = [data objectForKey:@"LogIn"];
NSLog(@"Longitude: %@", [currentConditions objectForKey@"Longitude"]);