ios - JSON format issue in PHP REST service receiving POST from AFNetworking 2.0 -


i have situation post json string ios code using afnetworking 2.0 code. client code looks this:

nsurl *url = [nsurl urlwithstring:baseurlstring]; nsstring *jsonstring = [[nsstring alloc] initwithdata:jsonpayloadforproject encoding:nsutf8stringencoding];  nsdictionary *parameters = @{@"project": jsonstring};  afhttpsessionmanager *manager = [[afhttpsessionmanager alloc]       initwithbaseurl:url]; manager.requestserializer = [afjsonrequestserializer serializer]; manager.responseserializer.acceptablecontenttypes = [nsset setwithobject:@"text/html"];  [manager post:@"service.php" parameters:parameters success:^(nsurlsessiondatatask *task, id responseobject) {     nslog(@"%@", [responseobject description]); } failure:^(nsurlsessiondatatask *task, nserror *error) {     uialertview *alertview = [[uialertview alloc] initwithtitle:@"post json project error"                                                         message:[error localizeddescription]                                                        delegate:nil                                               cancelbuttontitle:@"ok"                                               otherbuttontitles:nil];     [alertview show]; }]; 

the json looks this:

{   "maps" : [     {       "markers" : [         {           "marker_tag" : -790386438,           "location_y" : 182,           "location_x" : 344         },         {           "marker_tag" : 451496476,           "location_y" : 267,           "location_x" : 150.5         }       ],       "image_string" : "ec817509-de49-4914-840e-5e407571f6ae.jpeg",       "order_num" : 0,       "name" : "blah1",       "id" : "ec817509-de49-4914-840e-5e407571f6ae"     },     {       "markers" : [         {           "marker_tag" : -79601351,           "location_y" : 377,           "location_x" : 486.5,           "image_id" : "blah2",           "map_id" : "146c1c09-5ae0-4e4c-83c8-b7eaa8f28a9a"         }       ],       "image_string" : "146c1c09-5ae0-4e4c-83c8-b7eaa8f28a9a.jpeg",       "order_num" : 0,       "name" : "1st floor",       "id" : "146c1c09-5ae0-4e4c-83c8-b7eaa8f28a9a"     }   ],   "longitude" : "-222.222222",   "latitude" : "33.33333" } 

when read json string on server in php code using

$json_str = file_get_contents('php://input'); 

when that, following string:

{ "project" = '{ \n \"maps\" : [ \n { \n \"markers\" : [ \n { \n \"marker_tag\n : = -790386438, \n .... 

my question when following, nothing.

$array_str = json_decode($json_str, true); 

what best way put array parse data? how can original json string sent client?

a couple of issues

  1. your afhttpsessionmanager using default afjsonresponseserializer merely overriding acceptablecontenttypes. if server code returning json , failing specify in content-type header.

    • if server not returning json, rather changing acceptablecontenttypes, should use afhttpsessionmanager

      manager.responseserializer = [afhttpresponseserializer serializer]; 
    • or have server return json , make sure specifies correct content-type header:

      <?php  $json_str = file_get_contents('php://input'); $json = json_decode($json_str, true);  header('content-type: application/json');  $result = array("received" => $json); echo json_encode($result);  ?> 

      having done that, can remove acceptablecontenttypes line client code altogether , continue use default afjsonresponseserializer.

  2. the demonstrated mechanism building parameters incorrect. creating json dictionary 1 item in it, string representation of json.

    instead of putting string representation within dictionary, suggest want put actual dictionary within dictionary. so, if have nsdata of json, convert nested nsdictionary/nsarray structure:

    nserror *error; id jsonpayload = [nsjsonserialization jsonobjectwithdata:jsonpayloaddata options:0 error:&error]; nsassert(jsonpayload, @"json parsing error: %@", error);  nsdictionary *parameters = @{@"project": jsonpayload}; 

    now, likely, little silly create nsdata using datawithjsonobject, turn around , extract out, rather you'd use original nsdictionary within parameters, illustrates point.

so, pulling together, assuming change server code return formatted json response, outlined above, client code becomes:

nserror *error; id jsonpayload = [nsjsonserialization jsonobjectwithdata:jsonpayloaddata options:0 error:&error]; nsassert(jsonpayload, @"json parsing error: %@", error);  nsdictionary *parameters = @{@"project": jsonpayload};  afhttpsessionmanager *manager = [[afhttpsessionmanager alloc] initwithbaseurl:url]; manager.requestserializer = [afjsonrequestserializer serializer]; // manager.responseserializer.acceptablecontenttypes = [nsset setwithobject:@"text/html"];  [manager post:@"service.php" parameters:parameters success:^(nsurlsessiondatatask *task, id responseobject) {     nslog(@"%@", [responseobject description]); } failure:^(nsurlsessiondatatask *task, nserror *error) {     nslog(@"error=%@", error); }]; 

if that, send json request server, parsed, inserted new associative array, converted json , returned client app, use default afjsonresponseserializer parse , log it.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -