0

我需要能够创建一个 JSON 对象(我使用的是 iOS 5 中的内置类)。应该发送的 JSON 是这样的:

 {"request_type":"<the request type (a string)>" "security_level":<the security level (an int)> "device_type":"<android or ios (a string)" "version":<the app version (android, ios on different version schemes) (a float)> "email":<the email address of the user sending the request (a string)> "blob":{<the contents of the actual request, encrypted/encoded according to the security level (a map)>}

我的问题是最后一部分,一个“blob”,它基本上只是另一个 JSON 对象,即

 {"display_name":"Jack Bower", "email":"jackb@gmail.com", "password":"roflcopter"}

(让我们忘记密码是明文)

我可以使用 NSDictionary 创建所有内容,只是不知道如何添加最后一部分。

我的猜测是使用 NSDictionary 创建第一个请求。然后使用另一个 NSDictionary 创建第二个 blob 请求。然后只需将第二个 blob NSDictionary 作为对象添加回初始 NSDictionary。

NSJSONSerialization 会理解我想要做什么吗?

4

1 回答 1

0

好的,我想现在是凌晨 5 点,我真的很愚蠢:

这是答案:

NSDictionary *blobData = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"email",userEmail,
                             @"password",userPassword,
                      nil];
NSString *blobString = [[NSString alloc]
                        initWithData:[NSJSONSerialization dataWithJSONObject:blobData options:kNilOptions error:&error]
                        encoding:NSUTF8StringEncoding];
NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"login",@"request_type",
                             0,@"security_level",
                             @"ios",@"device_type",
                             @"blob",blobString,
                             nil];
NSData *JSONRequestData = NULL;
if ([NSJSONSerialization isValidJSONObject:requestData]) {
        NSData *JSONRequestData = [NSJSONSerialization dataWithJSONObject:requestData options:kNilOptions error:&error];
}
else NSLog(@"requestData was not a proper JSON object");
于 2012-08-01T09:51:19.483 回答