objective c - PHP flattening JSON array of objects? -
i'm using afnetworking post json ios app. seems working fine, json i'm sending has following format:
{ "user":{ "firstname":"joe", "lastname":"blogs", "contact":{ "email":"joe@blogs.com", "phone":"0800900800" }, "list":[ { "name":"item1", "code":"itm1", "category":0 }, { "name":"item2", "code":"itm2", "category":3 }, { "name":"item3", "code":"itm3", "category":2 } ] } }
i parse contents of json in mysql table. i'm able read info (firstname, lastname, contact etc) you'd expect json in php:
<?php $json = $_post["user"]; $fname = $json["firstname"]; $lname = $json["lastname"]; $contact = $json["contact"]; $email = $contact["email"]; . . . ?>
however when come iterate on "list" array, array appears flattened. if perform count:
$list = $json["list"]; $listcount = count($list);
$listcount
equal 9 (i.e. 3 objects in array x 3 properties on every object, if array has been flattened), rather 3 expect.
am misunderstanding how json arrays parsed in php or afnetworking processing json in way before posts?
update:
having var_dumped "list" array part of json , back:
["list"]=> array(9) { [0]=> array(1) { ["name"]=> string(5) "item1" } [1]=> array(1) { ["code"]=> string(4) "itm1" } [2]=> array(1) { ["category"]=> string(1) "0" } [3]=> array(1) { ["name"]=> string(5) "item2" } [4]=> array(1) { ["code"]=> string(4) "itm2" } [5]=> array(1) { ["category"]=> string(1) "3" } [6]=> array(1) { ["name"]=> string(5) "item3" } [7]=> array(1) { ["code"]=> string(4) "itm3" } [8]=> array(1) { ["category"]=> string(1) "2" } }
so looks if each object in original array has been split out in it's own object in array.
for reference here's request using afnetworking in ios app:
user *user = [users objectatindex:0]; nsdictionary *userdictionary = [user serialiseuser]; nslog(@"user\n%@", userdictionary); nsurl *url = [nsurl urlwithstring:baseurl]; afhttprequestoperationmanager *operationmanager = [[afhttprequestoperationmanager alloc] initwithbaseurl:url]; if (operationmanager) { [operationmanager post:path parameters:userdictionary success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"success: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"failed: %@", error); nslog(@"%@", operation.responsestring); }];
where serialiseuser
produces original json/dictionary object above.
thanks in advance insight.
try decoding json below
<?php $json = json_decode($_post["user"]); print_r($json); ?> stdclass object ( [user] => stdclass object ( [firstname] => joe [lastname] => blogs [contact] => stdclass object ( [email] => joe@blogs.com [phone] => 0800900800 ) [list] => array ( [0] => stdclass object ( [name] => item1 [code] => itm1 [category] => 0 ) [1] => stdclass object ( [name] => item2 [code] => itm2 [category] => 3 ) [2] => stdclass object ( [name] => item3 [code] => itm3 [category] => 2 ) ) ) )
Comments
Post a Comment