以Json格式iOS发布请求

时间:2015-04-27 05:11:52

标签: ios json ios7

我是iOS的新手,我必须以json格式发送帖子请求。喜欢,

  {"user":{"email":"abc@gmail.com"}}

我的编码就像,

NSMutableDictionary *postDict = [[NSMutableDictionary alloc]init];
[postDict setValue:@"test@gmail.com" forKey:@"email"];
//  [postDict setValue:text_password.text forKey:@"password"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];
NSString *stringData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

我正在获得输出

  {"email":"test@gmail.com"}

请告诉我如何添加{"user":

谢谢。

4 个答案:

答案 0 :(得分:2)

If you want - {"user":{"email":"abc@gmail.com"}}

然后你需要两个词典。

内部词典的第一个 - {“email”:“abc@gmail.com”}。和

外部字典的第二个 - {“user”:{....}}

首先创建内部词典。例如 -

NSMutableDictionary *data=[[NSMutableDictionary alloc]init];
[data setValue:@"abc@gmail.com" forKey:@"email"];

现在创建外部词典。例如 -

NSMutableDictionary *userInfo=[[NSMutableDictionary alloc]init];   
[userInfo setValue:data forKey:@"user"];

现在创建JSON对象 -

NSData *JSONData=[[NSData alloc] init];
if([NSJSONSerialization isValidJSONObject:userInfo])
{
    JSONData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:nil];
}
NSString *jsonString = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
NSLog(@"JSON Output: %@", jsonString); 

输出将是 -     { “用户”:{ “电子邮件”: “abc@gmail.com”}}

希望它会对你有所帮助。

答案 1 :(得分:0)

1)使用默认功能发布数据: -

        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setValue:@"test@gmail.com" forKey:@"email"];


        NSString *post =[dict JSONRepresentation];

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        //NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%ld",(long)[postData length]];

        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

        [request setURL:[NSURL URLWithString:[NSString stringWithFormat:”YourURL”]]];

      //  [request setTimeoutInterval:30.0f];

        [request setHTTPMethod:@"POST"];

        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];

        [request setHTTPBody:postData];

        NSURLConnection * connection = [[NSURLConnection alloc]
                                        initWithRequest:request
                                        delegate:self startImmediately:NO];

2)使用AFNetworking Library发布数据: -

  NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@"test@gmail.com" forKey:@"email"];


    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@“Your URL”]]];
    [request setHTTPMethod:@"POST"];

    NSString *post =[dict JSONRepresentation];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%ld",(long)[postData length]];

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
    [request setHTTPBody:postData];

    //Add your request object to an AFHTTPRequestOperation
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) {

         NSString *response = [operation responseString];
         NSLog(@"response: %@",response);

     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"error: %@", [operation error]);
     }];

    //call start on your request operation
    [operation start];

答案 2 :(得分:0)

我建议以更动态的方式提升你的思维,因为它很容易做到,但对于你的问题,你可以通过这样的代码实现它,

NSMutableDictionary *postDict =[[NSMutableDictionary alloc] init];
NSMutableDictionary *dicEmail =[[NSMutableDictionary alloc] init];
[dicEmail setObject:@"abc@gmail.com" forKey:@"email"];
[postDict setObject:dicEmail forKey:@"user"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];
NSString *stringData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

享受编码!!

答案 3 :(得分:0)

再制作一个字典并将第一个字典传递给第二个你得到格式的字典

google.maps.event.addDomListener(window, 
                                 'load', 
                                 function(){//context is window
                                   gmaps.initMap();//context is gmaps
                                 });

希望这是有帮助的