首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jsonParser objectWithString返回空值

jsonParser objectWithString返回空值
EN

Stack Overflow用户
提问于 2014-02-01 05:25:16
回答 1查看 351关注 0票数 0

在.h文件中声明

代码语言:javascript
复制
 NSString *extractUsersGRC;

.m文件

代码语言:javascript
复制
{
  ..
     extractUsersGRC=[[NSString alloc]init]; 
     extractUsersGRC = [[resultsGRC objectForKey:@"d"] retain];

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

     dict1=[[extractUsersGRC  JSONValue]retain];
 }

  • 我使用json从web服务获取数据,web服务可以重放我的请求,但有时我会将dict1作为零。jsonvalue返回我犯错误的null.So。
  • extractUsersGRC保存数据,但Jsonvalue返回null..?为什么?我没有得到帮助。

代码语言:javascript
复制
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 ];
EN

回答 1

Stack Overflow用户

发布于 2014-02-01 06:08:16

@property (nonatomic, strong) NSMutableData *returnData;文件中声明.h并跟随我

更改NSURLConnection声明

代码语言:javascript
复制
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection)
  self.returnData = [[NSMutableData alloc] init];
else
  NSLog(@"Connection Failed!");

NSURLConnection的委托方法

代码语言:javascript
复制
- (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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21494461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档