转换 NSArray -> JSON -> NSData -> PHP 服务器 -> JSON 表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10290767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting NSArray -> JSON -> NSData -> PHP server ->JSON representation
提问by Eric
I am converting an Array of NSDictionaries to JSON data with the following...
我正在使用以下内容将 NSDictionaries 数组转换为 JSON 数据...
Create my data...
创建我的数据...
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];
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...
这是将 NSData 发送到我的服务器,但是我如何在 php 中使用它......如果我从我的服务器打印它,它实际上只是一堆随机数,我已经尝试使用 json_encode 但我不认为那是为了原始数据...
Any help would be great!
任何帮助都会很棒!
EDIT: Here is the Response of php...
编辑:这是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>
Here is the response to NSLoging in Xcode...
这是对 Xcode 中 NSLoging 的响应...
NSLog(@"Info: %@", info);
Info: ( { artist = MyArtist; title = MySong; }, { artist = MyArtist; title = MySong; } )
信息:({艺术家=我的艺术家;标题=我的歌曲;},{艺术家=我的艺术家;标题=我的歌曲;})
回答by Eric
Turns out I needed to do it like this:
原来我需要这样做:
To Create My data:
创建我的数据:
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];
And Sent it like this:
并像这样发送:
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];
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.
关键是将信息转换为NSData,然后转换为我发送到服务器的 JSON 字符串,而不仅仅是发送原始NSData.
Thanks everyone for the help!
感谢大家的帮助!
回答by QED
<?php $array = json_decode($_POST['songs']); ?>should work.
<?php $array = json_decode($_POST['songs']); ?>应该管用。
回答by borrrden
You can't send it this way. NSJSONSerialization is for iOS use. It is basically a plist. You will need a way to decode it on PHP and I doubt it exists. Instead, you need to send a JSON string. I am not sure what you mean by "leaves quotes around titles." What are they doing there in the first place? Can you verify your original JSON string here?
你不能用这种方式发送它。NSJSONSerialization 供 iOS 使用。它基本上是一个plist。您将需要一种在 PHP 上解码它的方法,我怀疑它是否存在。相反,您需要发送一个 JSON 字符串。我不确定您所说的“在标题周围留下引号”是什么意思。他们首先在那里做什么?你能确认你原来的JSON字符串在这里?

