jsonParser objectWithString返回null值

时间:2014-02-01 05:25:17

标签: ios iphone objective-c json nsdictionary

在.h文件中声明

 NSString *extractUsersGRC;

.m文件

{
  ..
     extractUsersGRC=[[NSString alloc]init]; 
     extractUsersGRC = [[resultsGRC objectForKey:@"d"] retain];

     NSDictionary *dict1 =[[NSDictionary alloc]init];

     dict1=[[extractUsersGRC  JSONValue]retain];
 }

enter image description here


  • 我正在使用json从Web服务和Web服务获取数据 重播我的请求,但有时我得 dict1 为零。 jsonvalue返回null。所以我犯了错误。

  • extractUsersGRC保存数据,但Jsonvalue返回null ..?为什么?我是 没有得到帮助我。

enter image description here

SBJSON  *jsonGRC = [SBJSON new];
    jsonGRC.humanReadable = YES;
    responseData = [[NSMutableData data] retain];

    NSString *service = @"/GET_Recent_Activity";


    NSString *flagval=@"C";

    double latval=[[[NSUserDefaults standardUserDefaults]valueForKey:@"LATITUDE"]doubleValue];
    double longval=[[[NSUserDefaults standardUserDefaults]valueForKey:@"LONGITUDE"]doubleValue];
    NSString *userid=[[NSUserDefaults standardUserDefaults]valueForKey:@"UserID"];
    long u_id= [userid longLongValue];
    NSLog(@"%ld",u_id);

    NSString *requestString = [NSString stringWithFormat:@"{\"flag\":\"%@\",\"current_Lat\":\"%f\",\"current_Long\":\"%f\",\"userid\":\"%ld\"}",flagval,latval,longval,u_id];

    NSLog(@"request string:%@",requestString);


    NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

    NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
    fileContentsGRC = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];

    urlLocGRC = [fileContentsGRC objectForKey:@"URL"];

    urlLocGRC = [urlLocGRC stringByAppendingString:service];
    NSLog(@"URL : %@",urlLocGRC);

    requestGRC = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLocGRC]];
    NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
    [requestGRC setHTTPMethod: @"POST"];
    [requestGRC setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [requestGRC setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [requestGRC setHTTPBody: requestData];


    NSError *respError = nil;
    NSData *returnData = [NSURLConnection sendSynchronousRequest: requestGRC returningResponse: nil error: &respError ];

1 个答案:

答案 0 :(得分:0)

@property (nonatomic, strong) NSMutableData *returnData;档案中声明.h 跟我来

更改您的NSURLConnection声明

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection)
  self.returnData = [[NSMutableData alloc] init];
else
  NSLog(@"Connection Failed!");

并委托NSURLConnection

的方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.returnData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.returnData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Connection failed." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *jsonString = [[NSString alloc] initWithData:self.returnData encoding:NSUTF8StringEncoding];
    NSMutableDictionary *jsonDictionary  = [jsonString JSONValue];
    NSLog(@"%@", jsonDictionary);
}