Json在Array中编码

时间:2016-10-19 00:51:47

标签: php arrays json

嗨,大家需要一些帮助。 所以我有一个API将json数据传递给另一个API。

消息/ json必须如下所示:

[{

"MsgId": 1,
      "MsgDate": "10/18/2016 11:10:23",
      "Location": "L10129",
      "TruckNo": "AFA6790",
      "TruckerNo": "T1694",
      "DriverName": "FRANKLIN PALOR GARCIA",
      "MobileNo": "1234567890",

      "Details": [{
        "ReferenceNo": "DUMMY_REF1",
        "Origin": "Sucat",
        "Destination": "AIMS-GEN Trias Cavite City",
        "Consignee": "Juan Dela Cruz",
        "ReferenceNo2": "DUMMY_REF2"
      },{
        "ReferenceNo": "DUMMY_REF3",
        "Origin": "Sucat",
        "Destination": "AIMS-GEN Trias Cavite City",
        "Consignee": "Juan Tamad",
        "ReferenceNo2": "DUMMY_REF4"
      }]
    }]

但是,我无法做到。它会返回内部服务器错误。 这是我的json示例代码:

$array[]=array(
                "MsgID"=>"1",
                "MsgDate"=>"10/18/2016 11:10:23",
                "Location"=>"L10129",
                "TruckNo"=>"AFA6790",

                "Details":[array(
                    "ReferenceNo"=>"DUMMY_REF1"
                )]
);

 echo json_encode($array);

有人可以帮我解决这个问题吗? 谢谢

1 个答案:

答案 0 :(得分:0)

<?php
$array[]=array(
                "MsgID"=>"1",
                "MsgDate"=>"10/18/2016 11:10:23",
                "Location"=>"L10129",
                "TruckNo"=>"AFA6790",

                "Details"=>array(array(
                    "ReferenceNo"=>"DUMMY_REF1"
                ))
);

 echo json_encode($array);

,输出

[{"MsgID":"1","MsgDate":"10\/18\/2016 11:10:23","Location":"L10129","TruckNo":"AFA6790","Details":[{"ReferenceNo":"DUMMY_REF1"}]}]