12

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

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...

Any help would be great!

EDIT: 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>

Here is the response to NSLoging in Xcode...

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

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

Eric
  • 5,671
  • 5
  • 31
  • 42
  • What does the string you receive on the PHP side look like? – deceze Apr 24 '12 at 01:54
  • Well, that's certainly not JSON. Seems like a problem in the way you're sending the data, not one with PHP. – deceze Apr 24 '12 at 03:00
  • well I cant figure out whats wrong then? Ill edit my question with how I create my object... – Eric Apr 24 '12 at 03:04
  • Ok I updated to show a very simple creation of data – Eric Apr 24 '12 at 03:09
  • Think I figured it out... I use json_decode(stripslashes($host_songs)) which gets rid of all those backslashes before decoding... – Eric Apr 24 '12 at 03:51
  • Uh, that "random data" you received is ASCII. Of course, "pretty printing" it threw in some extraneous newline characters, but (without bothering to drag out an ASCII table) it looks pretty much like your input. – Hot Licks Apr 25 '12 at 21:05

3 Answers3

20

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.

Thanks everyone for the help!

gklka
  • 2,459
  • 1
  • 26
  • 53
Eric
  • 5,671
  • 5
  • 31
  • 42
  • Why did you convert to NSData in the first place? Use the interface that gives you a JSON character string instead. – Hot Licks Apr 25 '12 at 21:08
  • Ah, I see -- the crummy Apple interface doesn't have an interface that will produce a JSON string. – Hot Licks Apr 25 '12 at 21:10
  • Yea its pretty dumb... Oh well, its what I get for trying to use Apples implementation instead of just using a 3rd party library! – Eric Apr 26 '12 at 00:15
1

<?php $array = json_decode($_POST['songs']); ?> should work.

QED
  • 9,803
  • 7
  • 50
  • 87
  • when i echo $array nothing is printed... But I know I am sending data... because when I echo $_POST['songs'] I get a ton of numbers printed out... – Eric Apr 24 '12 at 02:04
  • Can you paste the numbers in this question...? Also, paste your Obj-C 'info' object using, simply: `NSLog(@"%@", info);` – QED Apr 24 '12 at 02:07
  • Well its thousands of lines of data... Would be too big to post. Basically if I print that same data from xcode rather than PHP but use NSString *string = NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; I can see the perfect json string representation of the data. – Eric Apr 24 '12 at 02:10
  • I can send the String representation instead, but it leaves quotes around titles with a space, so decode doesnt work.. have any ideas? – Eric Apr 24 '12 at 02:56
  • According to the docs, this is for a JSON string, not raw data. – borrrden Apr 24 '12 at 03:37
1

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?

borrrden
  • 33,256
  • 8
  • 74
  • 109
  • I am now sending the string (it passes validation)... But the string is sent like.. [ {"title" : "MySong"} ] and is received from php like [ { \"title\" : \"MySong\" } ] and I think the "\" are messing up the decoding? – Eric Apr 24 '12 at 03:48
  • Think I figured it out... I use json_decode(stripslashes($host_songs)) which gets rid of all those backslashes before decoding.. – Eric Apr 24 '12 at 03:52
  • It is being escaped. You are sending this via POST? Quotations are not URL safe. You need to cleanse the string first: Change " to %22 – borrrden Apr 24 '12 at 03:52
  • Or you could do that...I have very little idea about PHP ^^; – borrrden Apr 24 '12 at 03:52
  • haha thanks, same here... But yea im using post. looks like its working! – Eric Apr 24 '12 at 03:54
  • Take a look at this page: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm JSON uses almost entirely "potentially unsafe" characters, so keep that in mind. – borrrden Apr 24 '12 at 03:58