为什么我的 JSON 响应对象没有加载到字典中?

How come my JSON response object is not getting loaded into the dictionary?

我正在测试 JSON POST。我有 post 工作。我现在正在尝试使用 JSON 响应对象。我按照一个例子加载到字典中。

NSMutableDictionary *innerJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error1];

innerJson好像没有可用的数据。我尝试了两种方法来注销所有密钥。

-(IBAction)trigger:(id)sender
{
    NSString *message = @"Test Message";
    NSString *number = @"xxxxxxxxx";
    NSString *adId = @"4321";
    NSString *idfv = @"1234";
    NSURL *someURLSetBefore = [NSURL URLWithString:@"http://localhost:3000/messaging"];
    NSLog(@"%@",someURLSetBefore);
    NSLog(message);
    //[[CTMessageCenter sharedMessageCenter]  sendSMSWithText:message serviceCenter:nil toAddress:number];
    //build an info object and convert to json
    NSDictionary *newDatasetInfo = [NSDictionary dictionaryWithObjectsAndKeys:adId, @"adId", idfv, @"idfv", nil];

    //convert object to data
    NSError *error = nil;
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:kNilOptions error:&error];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:someURLSetBefore];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPBody:jsonData];

    // print json:
    NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
                                                     encoding:NSUTF8StringEncoding]);
    //NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    //[connection start];
    [NSURLConnection
     sendAsynchronousRequest:request
     queue:[[NSOperationQueue alloc] init]
     completionHandler:^(NSURLResponse *response,
                         NSData *data,
                         NSError *error) {
         NSLog(@"error: %@", error);
         NSLog(@"data: %@", data);
         NSLog(@"response: %@", response);

         NSError *error1;
         NSMutableDictionary *innerJson = [NSJSONSerialization
                                            JSONObjectWithData:data options:kNilOptions error:&error1
                                            ];
         NSLog(@"allKeys");
         for( NSString *aKey in [innerJson allKeys] )
         {
             // do something like a log:
             NSLog(aKey);
         }

         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
         if ([data length] >0 && error == nil && [httpResponse statusCode] == 200)
         {
         NSLog(@"dataAsString %@", [NSString stringWithUTF8String:[data bytes]]);
             // DO YOUR WORK HERE
             if ([innerJson objectForKey:@"status"]) {
                 // contains key
                 NSLog(@"status exists");
             }
         }

     }];
}

这是日志

2015-03-21 19:01:39.140 MayDay[70643:367300] http://localhost:3000/messaging
2015-03-21 19:01:39.140 MayDay[70643:367300] Test Message
2015-03-21 19:01:39.140 MayDay[70643:367300] JSON summary: {"adId":"4321","idfv":"1234"}
2015-03-21 19:01:39.256 MayDay[70643:367351] error: (null)
2015-03-21 19:01:39.256 MayDay[70643:367351] data: <227b2073 74617475 73203a20 276f6b27 207d22>
2015-03-21 19:01:39.257 MayDay[70643:367351] response: <NSHTTPURLResponse: 0x7ff8a1e363c0> { URL: http://localhost:3000/messaging } { status code: 200, headers {
    Connection = "keep-alive";
    "Content-Length" = 19;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Sat, 21 Mar 2015 23:01:39 GMT";
    "Set-Cookie" = "connect.sid=s%3A0-PALFYCrFKdwL5_k1bNaRhrz811XcFi.ki24wSPdIOua6OiWDSkO2%2FlSvaA%2B9ZBg1TkxF9Ou4b4; Path=/; HttpOnly";
    Vary = "X-HTTP-Method-Override, Accept-Encoding";
    "X-Content-Type-Options" = nosniff;
    "X-Download-Options" = noopen;
    "X-Frame-Options" = SAMEORIGIN;
    "X-XSS-Protection" = "1; mode=block";
} }
2015-03-21 19:01:39.257 MayDay[70643:367351] allKeys
2015-03-21 19:01:39.257 MayDay[70643:367351] dataAsString "{ status : 'ok' }"

更新

记录错误 1

error1 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7f8af9f1b7a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

如果这是您收到的回复:

{ status : 'ok' }

那么它看起来类似于 JSON,但它不是 JSON。

正确的 JSON 应该是:

{ "status" : "ok" }

否 JSON 解析器将接受您收到的响应。字符串状态必须在双引号内,字符串 'ok' 也必须在双引号内。