PHP展平JSON对象数组?

时间:2014-08-18 10:05:43

标签: php objective-c arrays json afnetworking

我使用AFNetworking从iOS应用中发布一些JSON。这似乎工作正常,我发送的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
            }
        ]
    }
}

然后我将此JSON的内容解析为MySQL表。我能够阅读所有信息(firstName,lastName,contact等),因为你期望从PHP中的JSON:

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

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

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

然而,当我来遍历&#34;列表&#34;数组,数组似乎是扁平的。如果我执行计数:

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

$listCount将等于9(即数组中的3个对象x每个对象上的3个属性,就像数组已被展平一样),而不是我期望的3。

我是否误解了如何在PHP中解析JSON数组,或者AFNetworking在发布之前是否会以某种方式处理JSON?

更新

让var_dumped&#34; list&#34; 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"
}
}

所以看起来好像原始数组中的每个对象都被分割成了这个数组中它自己的对象。

此处参考我在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);
                   }];

serialiseUser生成上面的原始JSON / Dictionary对象。

提前感谢任何见解。

1 个答案:

答案 0 :(得分:1)

尝试解码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
                    )

            )



       )

)