POST NSDictionary或NSArray到JSON吗? [英] POST NSDictionary or NSArray into JSON?

查看:137
本文介绍了POST NSDictionary或NSArray到JSON吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将NSArray或NSDictionary值发布到JSON.是否可能.现在我已经将NSDictionary值和NSArray值转换为JSON.如何从JSON获取我的响应?以及如何从该响应中获取值?

How to POST NSArray or NSDictionary values to JSON.Its possible or not.Now i have done convert NSDictionary values and NSArray values into JSON.But I am connecting with PHP my php code having encode and decode method with out encode and decode how can get my response from JSON? and How can get the values from that response?

我尝试将以下代码转换为JSON格式:

I have tried to convert JSON format following codes::

 // Empty array
    NSArray *emptyArray = @[];

    // Single element array
    NSArray *singleElementArray = @[@"Error Message"];

    // Array of strings
    NSArray *arrayOfStrings = @[@"First Name", @"Last Name"];

    // Array of above arrays
    NSArray *arrayOfObjects = @[emptyArray, singleElementArray, arrayOfStrings];

    // Dictionary with several kay/value pairs and the above array of arrays
    NSDictionary *dict = @{@"BlogName" : @"iOS Developer Tips",
                           @"BlogDomain" : @"iOSDeveloperTips.com",
                           @"Array" : arrayOfObjects};

    NSError *error = nil;
    NSData *json;

    // Dictionary convertable to JSON ?
    if ([NSJSONSerialization isValidJSONObject:dict])
    {
        // Serialize the dictionary
        json = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

        NSLog(@"json: %@", json);


        // If no errors, let's view the JSON
        if (json != nil && error == nil)
        {
          NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];

          NSLog(@"JSON conversion: %@", jsonString);


        }
    }

我从jsonString中得到了正确的值.我已经将该字符串发布到JSON POST方法中,它的工作正常,但是我需要将NSDictionary或NSArray发布到JSON.HOW?

I got Correct values from jsonString .I have post this string to JSON POST method its working fine but i need to NSDictionary or NSArray are post to JSON.HOW?

推荐答案

最后,我得到了解决方案::

Finally i got the solutions::

// iOS 5 Json序列化

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"value1",@"username",@"value2",@"password", nil];
NSLog(@"dict :: %@",dict);// What ever  parametere you want send value and key pair in this dictionary.
NSError *error2;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error2];
NSString *post = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"postLength :: %@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://localhost:8000/api/v1/login/"]];// URL post URl change here.
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSError *error3;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error3];

NSString *str = [[NSString alloc] initWithData:POSTReply encoding:NSUTF8StringEncoding];
NSLog(@"str :: %@",str);

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:POSTReply
                                                     options:NSJSONReadingMutableContainers
                                                       error:&error3];

NSLog(@"parsedvalue%@",json);

这篇关于POST NSDictionary或NSArray到JSON吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆