php - 转换 NSArray -> JSON -> NSData -> PHP 服务器 ->JSON 表示

标签 php objective-c ios json asiformdatarequest

我正在将一个 NSDictionaries 数组转换为 JSON 数据...

创建我的数据...

NSMutableArray *arrayOfDicts = [[NSMutableArray alloc] init];

for (int i = 0; i < 2; i++) {
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"MySong", @"title",
                                  @"MyArtist", @"artist",
                                  nil];
    [arrayOfDicts addObject:dict];         
}
 NSArray *info = [NSArray arrayWithArray:arrayOfDicts];
 NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info 
      options:NSJSONWritingPrettyPrinted error:&error];

然后像这样发送...

NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/index.php"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:jsonData forKey:@"songs"];
    [request setDelegate:self];
    request.tag = TAG_LOAD_SONGS;
    [request startAsynchronous];

这是将 NSData 发送到我的服务器,但我如何在 php 中使用它...如果我从我的服务器打印它,它实际上只是一堆随机数,我已经尝试使用 json_encode 但我不认为这意味着原始数据......

任何帮助都会很棒!

编辑: 这是 php 的响应...

echo $_POST['songs'];

<5b0a2020 7b0a2020 20202274 69746c65 22203a20 224d7953 6f6e6722 2c0a2020 20202261 72746973 7422203a 20224d79 41727469 7374220a 20207d2c 0a20207b 0a202020 20227469 746c6522 203a2022 4d79536f 6e67222c 0a202020 20226172 74697374 22203a20 224d7941 72746973 74220a20 207d0a5d>

这是 Xcode 中对 NSLoging 的响应...

NSLog(@"Info: %@", info);

信息:( { 艺术家=我的艺术家; 标题=我的歌; }, { 艺术家=我的艺术家; 标题=我的歌; } )

最佳答案

原来我需要这样做:

创建我的数据:

NSMutableArray *arrayOfDicts = [[NSMutableArray alloc] init];

for (int i = 0; i < 2; i++) {
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"MySong", @"title",
                                  @"MyArtist", @"artist",
                                  nil];
    [arrayOfDicts addObject:dict];         
}
 NSArray *info = [NSArray arrayWithArray:arrayOfDicts];

然后像这样发送:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info 
          options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    // Start request
    NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/index.php"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:jsonString forKey:@"songs"];
    [request setDelegate:self];
    [request startAsynchronous];

关键是将信息转换为 NSData,然后转换为我发送到服务器的 JSON 字符串,而不仅仅是发送原始 NSData

感谢大家的帮助!

关于php - 转换 NSArray -> JSON -> NSData -> PHP 服务器 ->JSON 表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10290767/

相关文章:

javascript - 网络套接字 : onmessage is not firing. !

php - 将 Yii2 GridView 与数据数组一起使用

ios - Objective-C 类扩展

ios - 如何在没有任何延迟的情况下立即运行 segue 之后的动画?

php - 添加列 codeigniter

objective-c - 调整照片库大小以供预览的最佳方式

objective-c - Cocoapod 无法在 Travis-CI 上构建 - "expected a type"

iOS Combine Framework - Publisher 只发布一次,然后再也不会发布

ios - 在警报 View 之前键盘不会被关闭

php - 使用 PHP 将推荐数量和推荐 ID 添加到 MySQL 数据库中