我正在尝试从 JSON 响应中获取已解析的对象并在方法中使用,当我正常执行此操作时,我可以在此方法之后使用 parsedObject [self.delegate requestJSONFinishedWithParsedObject:parsedObject]; 我使用,但现在我想要这个 parsedObject 我想要的。我的意思是调用一个方法并在返回中包含对象。
像这样的东西:
NSDictionary *parsedObject = [self.delegate responseJsonDictionary]; 在我需要的课堂上使用它。
我的问题是我有一个@property (strong, nonatomic) NSMutableArray *projectsArray;
并且我想在使用它之前完成这个属性。我正在使用来自 JSON 的项目创建一个动态菜单,但是当来自 JSON 的数据位于 self.projectsArray 中时,我的菜单已经创建并且 json 的对象没有出现。
我正在使用这个幻灯片菜单:https ://github.com/andrewroycarter/SlideViewController
我使用这个类来管理所有的请求,它工作得很好。
请求JSON.m
-(void) requestWithURL:(NSDictionary *)jsonDictionary :(NSString*)myURL :(NSString *)method
{
NSURL *thisURL = [NSURL URLWithString:myURL];
NSString *__jsonString;
NSData *__jsonData;
if([NSJSONSerialization isValidJSONObject:jsonDictionary])
{
__jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
__jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding];
}
// Be sure to properly escape your url string.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:thisURL];
[request setHTTPMethod:method];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"Carlos" forHTTPHeaderField:@"User"];
if([method isEqual: @"POST"]){
[request setValue:[NSString stringWithFormat:@"%d", [__jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: __jsonData];
}
NSLog(@"llega= asdasdasd %@, %@, %@",method,jsonDictionary,myURL);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//INicializo variable NSMutableData
self.myConnectionData= [NSMutableData data];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *myResponse = [[NSDictionary alloc] init];
myResponse=[httpResponse allHeaderFields];
statusCode = [httpResponse statusCode];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.myConnectionData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self.delegate requestJSONFailed];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *thisError;
NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:myConnectionData options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&thisError];
[self.delegate requestJSONFinishedWithParsedObject:parsedObject];
}
@end
请求JSON.h
@protocol RequestJSONDelegate;
@interface RequestJSON : NSObject
{
NSInteger statusCode;
}
@property (nonatomic,assign) id <RequestJSONDelegate> delegate;
@property (nonatomic, strong) NSMutableData *myConnectionData;
-(void) requestWithURL:(NSDictionary *) params:(NSString*)myURL :(NSString *)method;
-(void) requestForLogin:(NSString*)myURL :(NSString*)method :(NSString*)pass;
@end
@protocol RequestJSONDelegate
-(void) requestJSONFinishedWithParsedObject:(NSDictionary*)parsedObject;
-(void) requestJSONFailed;
@end
在我想要获取此数据的其他类中,我使用此方法
-(void) requestJSONFinishedWithParsedObject:(NSDictionary *)parsedObject{
NSArray *projectsObjectArray = [parsedObject objectForKey:@"projects"];
}
-(void) requestJSONFailed{}
为了开始请求,我在我的主课中使用了它。
例子.m
RequestJSON *requestJSON = [ [RequestJSON alloc] init];
[requestJSON requestWithURL:(NSDictionary *) params:url :method];
[requestJSON setDelegate:self];
谢谢