UItextFields数据到JSON

时间:2012-05-14 09:19:49

标签: objective-c ios xcode uitextfield

我有两个UItextFields,userNameTextField和passwordTextField以及一个UIbutton连接。我想拥有用户在userNametextField和passwordTextField中输入的内容,我怎么能这样做呢?谢谢你的回答。

#pragma mark - User auhtentification
-(IBAction)userAuthentificate{
    BOOL loginValid = YES;
    BOOL passwordValid = YES;   // Not specified yet

    // contrioll d'interfac__ on test login et password
    if (loginValid && passwordValid) {

    NSString *jsonRequest = @"{\"login\":\"Samir\",\"password\":\"test\",\"mail\":\"test@gmail.com\"}";


        //  NSString *jsonRequest = @"{\"login\":\"Samir\",\"password\":\"test\",\"mail\":\"samir@gmail.com\",\"editor\":\"1\"}";


        NSString *request_url =@"http:.../register"; // Url WS
        NSURL *url = [NSURL URLWithString:request_url];
        self.currentRequest = [ASIFormDataRequest requestWithURL:url];
        currentRequest.delegate =self;
        NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
        [self.currentRequest appendPostData:requestData];
        [self.currentRequest setRequestMethod:@"POST"];

        [self.currentRequest startSynchronous];
    }
}

3 个答案:

答案 0 :(得分:2)

您可以在jsonRequest字符串的位置放置此类内容(代码使用SBJson框架):

NSDictionary *container = [[NSDictionary alloc] initWithObjectsAndKeys:
                            [loginTextField text], @"login",
                            [passwordTextField text], @"password",
                            [mailTextField text], @"mail"];

NSString *jsonString = [container JSONRepresentation];
// don't forget to release container if not using ARC AFTER creating the NSData object

答案 1 :(得分:1)

简单的字符串替换怎么样?

NSString *jsonRequest = [NSString stringWithFormat:@"{\"login\":\"%@\",\"password\":\"%@\",\"mail\":\"%@\"}", userNameTextField.text, passwordTextField.text, emailtextField.text];

答案 2 :(得分:0)

使用

NSString *jsonRequest = [NSString stringWithFormat:@"{\"login\":\""%@"\",\"password\":\""%@"\",\"mail\":\"test@gmail.com\"}",userNameTextField.text,passwordTextField.text];