简体   繁体   中英

PHP flattening JSON array of objects?

I'm using AFNetworking to POST some JSON from an iOS app. This seems to be working fine, the JSON i'm sending has the 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 then parse the contents of this JSON in to a MySQL table. I'm able to read all the info (firstName, lastName, contact etc) as you'd expect from the JSON in PHP:

<?php
    $json = $_POST["user"];

    $fname = $json["firstName"];
    $lname = $json["lastName"];

    $contact = $json["contact"];
    $email = $contact["email"];
    .
    .
    .
?>

However when I come to iterate over the "list" array, the array appears to be flattened. If I perform a count:

$list = $json["list"];
$listCount = count($list);

$listCount will equal 9 (ie 3 objects in the array x 3 properties on every object, as if the array has been flattened), rather than the 3 I would expect.

Am I misunderstanding how JSON arrays are parsed in PHP or could AFNetworking be processing the JSON in some way before is posts?

UPDATE:

Having var_dumped the "list" array part of the JSON and I get this 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 it looks as if each object in the original array has been split out in to it's own object in this array.

For reference here's my request using AFNetworking in the 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 the original JSON/Dictionary object above.

Thanks in advance for any insight.

Try decoding the json like 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
                    )

            )



       )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM