0

我正在向服务器请求一些应该以JSON格式返回的结果。

当我打印出请求的字符串时,它显示正确的输出。但是,当我尝试解析结果字符串时,出现错误:

Error Domain=org.brautaset.JSON.ErrorDomain Code=3 "Object value expected for key: result" UserInfo=0x6c4cbe0 {NSUnderlyingError=0x6c4caf0 "Object value expected for key: map", NSLocalizedDescription=Object value expected for key: result}

这是我的 JSON 代码:

SBJSON *jsonParser = [[SBJSON alloc] init];

[jsonParser setHumanReadable:YES];
NSError *theError;

NSString *loginPayload = @"{\"service\": \"getMyJobs\", \"params\": {\"map\": {\"day\": {\"javaClass\": \"java.sql.Timestamp\", \"time\": 1316757600000}}}, \"security\": {\"map\": {}}, \"userInfo\": false}";

NSURL * url = [NSURL URLWithString:kURLForAPI];

NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[loginPayload dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPShouldHandleCookies:YES];
[req setValue:@"text/xml  charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[req setValue:cookie forHTTPHeaderField:@"Cookie"];

NSHTTPURLResponse * response = nil;
NSData * dataResult = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&theError];
NSMutableDictionary *dicts;

if (dataResult) {
    NSString *resultString = [[NSString alloc] initWithData:dataResult encoding:NSUTF8StringEncoding];
    //NSLog(@"login response: %@",resultString);
    //NSLog(@"response %@", [response allHeaderFields]);

    dicts = [jsonParser objectWithString:resultString error:&theError];
    //dicts = [resultString JSONValue];
    if(theError)
    {
        NSLog(@"Result %@",[theError description]);
    }
4

2 回答 2

0

以此格式创建日期。

   NSStime *time = [NSString stringWithFormat:@"/Date(%@)/", [NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970] * 1000]];
于 2011-12-21T14:43:47.860 回答
0

不要使用 NSString 创建 JSON 请求正文。使用 [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 然后就做 [dictionaryinput JSONRepresentation]; 它将为您创建 JSON 字符串。这是序列化和反序列化 JSON 对象的最佳方式,并且会省去很多麻烦。

于 2011-12-21T01:05:06.810 回答