我只是从IOS开始并为我的计算机科学类创建一个IOS应用程序(最终项目),它使用JSON从远程MySql数据库获取数据。
这是我从ViewDidLoad调用的方法:
- (void)loadJSON
{
//-- Make URL request with server
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:@"http://vito-service.ru/studentsconnect/profile.php"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//-- Get request and response though URL
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//-- JSON Parsing
NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Result = %@",result);
}
和NSLog打印正确的数据:
Result = {
users = (
{
IdUser = 1;
facebook = "";
fullname = "";
instagram = "";
phonenumber = "";
username = moderas;
},
{
IdUser = 2;
facebook = "fb.com/petroffmichael";
fullname = "Michael Perov";
instagram = "@petroffmichael";
phonenumber = "(650)557-6168";
username = petroffmichael;
},
{
IdUser = 3;
facebook = "fb.com/testuser";
fullname = "Test User";
instagram = "@testuser";
phonenumber = "111 111-1111";
username = testuser;
}
);
}
但是如何将这些数据作为字符串检索以将它们放到标签上呢?我尝试了很多不同的创建数据对象的选项,我在这里找到堆栈溢出但是我总是得到一个"无法识别的选择器发送到实例"错误。我也试过创建一个单独的NSObject
@synthesize IdUser, username, fullname, phonenumber, facebook, instagram;
- (id) initWithUsername: (NSString *)uUsername andUserFullName: (NSString *)uFullname andUserPhonenumber: (NSString *)uPhonenumber andUserFacebook: (NSString *)uFacebook andUserInstagram: (NSString *)uInstagram andUserId: (NSString *)uId
{
self = [super init];
if (self) {
username = uUsername;
fullname = uFullname;
phonenumber = uPhonenumber;
facebook = uFacebook;
instagram = uInstagram;
IdUser = uId;
}
return self;
}
然后使用此方法检索数据:
-(void)retrieveData
{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
usersArray = [[NSMutableArray alloc] init];
for (int i=0; i<jsonArray.count; i++) {
NSString * uId = [[jsonArray objectAtIndex: i] objectForKey:@"IdUser"];
NSString * uUsername = [[jsonArray objectAtIndex: i] objectForKey:@"username"];
NSString * uFullname = [[jsonArray objectAtIndex: i] objectForKey:@"fullname"];
NSString * uPhonenumber = [[jsonArray objectAtIndex: i] objectForKey:@"phonenumber"];
NSString * uFacebook = [[jsonArray objectAtIndex: i] objectForKey:@"facebook"];
NSString * uInstagram = [[jsonArray objectAtIndex: i] objectForKey:@"instagram"];
[usersArray addObject:[[User alloc] initWithUsername:uUsername andUserFullName:uFullname andUserPhonenumber:uPhonenumber andUserFacebook:uFacebook andUserInstagram:uInstagram andUserId:uId]];
}
}
我有所有重要的#import
&#39;但我仍然会遇到同样的错误:
2014-04-23 18:35:26.612 testimage[47155:70b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa72a8b0
2014-04-23 18:35:26.813 testimage[47155:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa72a8b0'
有人可以帮忙找到合适的解决方案吗? 谢谢!
我想我还在做错事
jsonArray不再是一个数组了:
@property (nonatomic, strong) NSDictionary *jsonArray;
@property (nonatomic, strong) NSMutableArray *usersArray;
然后方法:
-(void)retrieveData
{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
usersArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in jsonArray)
{
NSString * uId = [dict objectForKey:@"IdUser"];
NSString * uUsername = [dict objectForKey:@"username"];
NSString * uFullname = [dict objectForKey:@"fullname"];
NSString * uPhonenumber = [dict objectForKey:@"phonenumber"];
NSString * uFacebook = [dict objectForKey:@"facebook"];
NSString * uInstagram = [dict objectForKey:@"instagram"];
[usersArray addObject:[[User alloc] initWithUsername:uUsername andUserFullName:uFullname andUserPhonenumber:uPhonenumber andUserFacebook:uFacebook andUserInstagram:uInstagram andUserId:uId]];
}
}
我仍然遇到同样的错误:
2014-04-23 20:44:43.971 testimage[48285:70b] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa5547d0
2014-04-23 20:44:44.053 testimage[48285:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa5547d0'
答案 0 :(得分:1)
仔细观察那个输出。它不是你所说的。你所拥有的不是一个数组 - 它是一个带有一个键@"users"
的字典,其值是一个数组。这是它在以{ users = …
开头时告诉你的内容。如果你想要数组,你需要从字典中检索它。
答案 1 :(得分:0)
试试这个:
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSMutableArray *result = [res objectForKey:@"users"];
这样做。响应不是NSArray。这是一本字典。首先从字典中获取数组,然后在使用时使用它。希望这有助于...:)
修改强>
您可以通过以下方式重写循环:
for (NSDictionary *dict in jsonArray)
{
//dict is a NSDictionary. Just get value for keys
}