使用多个级别在PHP中创建JSON

时间:2015-08-14 08:44:36

标签: php json

我需要通过PHP创建多个级别的json。

{
    "accident": {
        "dateTime": "2015-08-08T16:12:08",
        "desc": "string"
    },
    "calculationInput": {
        "idBlockCodes": [
            {
                "code": 51,
                "value": 1
            }
        ],
        "wageRates": {
            "labourRate1": 10,
            "labourRate2": 11,
            "labourRate3": 22,
            "labourRate4": 33,
            "labourRateD": 44,
            "paintRate": 55
        }
    },
    "caseNumber": "F4Q",
    "vehicle": {
        "adminData": {
            "firstRegDate": "2015-07-08",
            "numberPlate": "MI214AF"
        },
        "identification": {
            "vin": "VF7S6NFZF56215577"
        },
        "owner": {
            "city": "Praha",
            "country": "CZE",
            "email": "mail@mail.com",
            "name": "Fero Taraba",
            "phone": "777111222",
            "phone2": "999555333",
            "postCode": "07101",
            "street": "Kukureliho 18"
        }
    }
}

我激活了这个,我可以创建单级json,换句话说:

$data = array (
        "caseNumber" =>"F4Q"    
        );

然后很简单:

$data_json= json_encode($data);

但有人可以解释一下我如何才能将第二级甚至第三级树转换为JSON?

我真的会帮助你的帮助人员。感谢

修改: 大多数答案确实引导我正确的方式,现在只有问题是这一部分:

  "idBlockCodes": [
                {
                    "code": 51,
                    "value": 1
                }
            ],

其中有[]代表某些blockCodes列表。不知道该怎么办? :)

5 个答案:

答案 0 :(得分:1)

你的问题到底是什么?

您可以创建一个数组,然后将其编码为json?

$data = array(
    'Key 1' => array(
         'Key 2 - Key 1' => array(
              'Key 3 - Key 1' => array()
          )
    )
);
echo json_encode($data);

答案 1 :(得分:1)

只需使用关联数组

$data = array (
        "caseNumber" =>"F4Q",    
        "vehicle" => array (
            "adminData"=>
                     array(
                      "firstRegDate"=> "2015-07-08",
                       "numberPlate"=> "MI214AF"
                     ),
              "identification" =>
                      array(
                      "vin"=> "VF7S6NFZF56215577"
                      )      
            )
        );

打印

{"caseNumber":"F4Q","vehicle":{"adminData":{"firstRegDate":"2015-07-08","numberPlate":"MI214AF"},"identification":{"vin":"VF7S6NFZF56215577"}}}

答案 2 :(得分:1)

使用关联数组:

$data = array (
    "caseNumber" =>"F4Q",    
    "vehicle" => array (
        "adminData"=>
                 array(
                  "firstRegDate"=> "2015-07-08",
                   "numberPlate"=> "MI214AF"
                 ),
          "identification" =>
                  array(
                  "vin"=> "VF7S6NFZF56215577"
                  )      
        )
    );

echo json_encode($data)

答案 3 :(得分:1)

通过在对象周围放置一个额外的数组来获得括号。

$data = array("idBlockCodes" => array(
            array(
                "code" => 51,
                "value" => 1
            )
        )
  );

 print json_encode($data);

答案 4 :(得分:0)

您可以将所有数据转换为数组:

您必须先使用json_encode();

   $a = json_encode($yourData);

然后解码它:

   $b = json_decode($a, true);
   echo '<pre>';
   print_r($b); //print it

它显示与之关联的数组