【问题标题】:POST NSDictionary or NSArray into JSON?将 NSDictionary 或 NSArray 发布到 JSON 中?
【发布时间】:2015-02-09 13:19:06
【问题描述】:

如何将 NSArray 或 NSDictionary 值发布到 JSON。它可能与否。现在我已经将 NSDictionary 值和 NSArray 值转换为 JSON。但是我正在使用 PHP 连接我的 php 代码,它具有编码和解码方法而没有编码和解码如何从 JSON 中获取我的响应?以及如何从该响应中获取值?

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

 // 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?

【问题讨论】:

  • 您需要显示 NSArray 中的内容以及您希望 JSON 的外观。一个模糊的问题有太多答案。

标签: ios json


【解决方案1】:

终于找到解决办法了::

//iOS 5 Json Serialization

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多