Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

objective c - Converting NSArray -> JSON -> NSData -> PHP server ->JSON representation

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...