简体   繁体   English

PHP展平对象的JSON数组?

[英]PHP flattening JSON array of objects?

I'm using AFNetworking to POST some JSON from an iOS app. 我正在使用AFNetworking从iOS应用中发布一些JSON。 This seems to be working fine, the JSON i'm sending has the following format: 这似乎工作正常,我发送的JSON具有以下格式:

{
    "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. 然后,我将此JSON的内容解析到MySQL表中。 I'm able to read all the info (firstName, lastName, contact etc) as you'd expect from the JSON in PHP: 我能够按照您期望的那样从PHP的JSON中读取所有信息(名字,姓氏,联系方式等):

<?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. $listCount将等于9(即数组中的3个对象x每个对象上的3个属性,就好像数组已经展平了),而不是我期望的3个。

Am I misunderstanding how JSON arrays are parsed in PHP or could AFNetworking be processing the JSON in some way before is posts? 我是否误解了如何在PHP中解析JSON数组,或者AFNetworking可以在发布之前以某种方式处理JSON?

UPDATE: 更新:

Having var_dumped the "list" array part of the JSON and I get this back: var_dumped JSON的“列表”数组部分后,我得到了以下信息:

["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: 供参考,这是我在iOS应用中使用AFNetworking的请求:

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. 其中serialiseUser产生上面的原始JSON / Dictionary对象。

Thanks in advance for any insight. 预先感谢您的任何见解。

Try decoding the json like below 尝试如下解码json

<?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
                    )

            )



       )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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