转换NSArray - > JSON - > NSData - > PHP服务器 - > JSON表示 [英] Converting NSArray -> JSON -> NSData -> PHP server ->JSON representation

查看:135
本文介绍了转换NSArray - > JSON - > NSData - > PHP服务器 - > JSON表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下内容将NSDictionaries数组转换为JSON数据...

I am converting an Array of NSDictionaries to JSON data with the following...

创建我的数据...

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

然后像这样发送......

then send like so...

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,但我不认为这意味着原始数据...

This is sending the NSData to my server, but how do I then use it in php... Its really just a bunch of random numbers if I print it from my server, and I have tried using json_encode but I dont think thats meant for raw data...

任何帮助会很棒!

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

Here is the Response of 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的回复...

Here is the response to NSLoging in Xcode...

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

信息:(
{
artist = MyArtist;
title = MySong;
},
{
artist = MyArtist;
title = MySong;
}

Info: ( { artist = MyArtist; title = MySong; }, { artist = MyArtist; title = MySong; } )

推荐答案

原来我需要这样做:

创建我的数据:

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

The key was to convert the info to NSData, then to a JSON String which I sent to my server, not just sending the raw NSData.

谢谢大家帮忙!

这篇关于转换NSArray - &gt; JSON - &gt; NSData - &gt; PHP服务器 - &gt; JSON表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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