如何在JSON中将[]作为字符串发送?

时间:2017-04-17 05:37:21

标签: php json codeigniter

在特定场景中,我想将INI文件内容作为JSON数据发送。

这是我的示例内容,我想在PHP Codeigniter中json_encode

[PRODUCT]
Username=9008
Password=45645646464654

[DialPlan]
DP_Exception=
DP_Rule1=
DP_Rule2=

[Advanced]
CellNumber=
Transport=
isAccountActive=1

[Device]
VirtualMAC=
ToolReset=
Mode=3"

我试过这种方式:

 $message =  array(
"Request" =>"AddUpdateBaseINI",
"GMT_Timestamp"=>$timestamp,
"SessionID"=>$session_id,
"tenant_id" =>"1",
"INI"=>"[PRODUCT]
Username=9008
Password=45645646464654

[DialPlan]
DP_Exception=
DP_Rule1=
DP_Rule2=

[Advanced]
CellNumber=
Transport=
isAccountActive=1

[Device]
VirtualMAC=
ToolReset=
Mode=3"

);

$request = json_encode($message);

但这不会产生预期的结果。 我如何通过" []"作为字符串而不是json对象。

预期输出

{
  "Request": "GetDefaultBaseINI",
  "GMT_Timestamp": "055110",
  "SessionID": "79r46pobm2ah2pk4bjnqs2f5k9g2ubjn",
  "INI": "[PRODUCT] Username=9008 Password=45645646464654 [DialPlan] DP_Exception= DP_Rule1= DP_Rule2= [Advanced] CellNumber= Transport= isAccountActive=1 [Device] VirtualMAC= ToolReset= Mode=3"
}

输出我得到$ _POST []:

数组([{"Request":"AddUpdateBaseINI","GMT_Timestamp":"064024","SessionID":"79r46pobm2ah2pk4bjnqs2f5k9g2ubjn","tenant_id":"1","INIID":null,"location_id":"2","product":"bizfms","INIContent":"] => Array ( [BIZFMS] => 9008 Password=45645646464654 Domain=172.24.130.201 Proxy=172.24.130.122 Port=5070 SipAuthName=45645646464654 DisplayName=Jayesh SiteKey=5DCA-E878 Proxy=172.24.130.122 [DialPlan]DP_Exception= DP_Rule1= DP_Rule2= [Advanced] EnterpriseVoiceMail= EnterpriseVoiceMailPin= CellNumber= SipTransport=\r\nDNSServer= 8.8.8.8 EnableDNS= RegistrationExpiry=3600 DTMFMode=\r\nKeepAliveWiFi=30 KeepAliveMobileData=60SipDSCP=30 RTPDSCP=28 SecureRTP=\r\nClientSipPort=5090 ConnectivityMode=1 WiFiDisconnectionAlert= StunServer= EnableIce= HoldType= Codec=9,8,0 MobileDataCodecs=9,8,0 PacketizationInterval=20 SoftwareAEC= EchoTailLength=300 ToneLocalization= EnableVideo=1 VideoFrame=9 InitialBitrate=500 MaximumBitrate= 1000 VideoResolution=1 VideoPort=19304 LogLevel=60000 RunInBackground=1 EdOption=1 EnsipOption=3 RingTone= DefaultisAccountActive=1 [Device]VirtualMAC= ToolReset=\r\nAudioMode= 3"} ) [0] => Array ( [BIZFMS] => 9008 Password=45645646464654 Domain=172.24.130.201 Proxy=172.24.130.122 Port=5070 SipAuthName=45645646464654 DisplayName=Jayesh SiteKey=5DCA-E878 Proxy=172.24.130.122 [DialPlan]DP_Exception= DP_Rule1= DP_Rule2= [Advanced] EnterpriseVoiceMail= EnterpriseVoiceMailPin= CellNumber= SipTransport=\r\nDNSServer= 8.8.8.8 EnableDNS= RegistrationExpiry=3600 DTMFMode=\r\nKeepAliveWiFi=30 KeepAliveMobileData=60SipDSCP=30 RTPDSCP=28 SecureRTP=\r\nClientSipPort=5090 ConnectivityMode=1 WiFiDisconnectionAlert= StunServer= EnableIce= HoldType= Codec=9,8,0 MobileDataCodecs=9,8,0 PacketizationInterval=20 SoftwareAEC= EchoTailLength=300 ToneLocalization= EnableVideo=1 VideoFrame=9 InitialBitrate=500 MaximumBitrate= 1000 VideoResolution=1 VideoPort=19304 LogLevel=60000 RunInBackground=1 EdOption=1 EnsipOption=3 RingTone= DefaultisAccountActive=1 [Device]VirtualMAC= ToolReset=\r\nAudioMode= 3"} ) )

2 个答案:

答案 0 :(得分:1)

您需要在INI file中使用heredoc字符串方法传递JSON,如下所示:

$string = <<<EOD
[PRODUCT]
Username=9008
Password=45645646464654

[DialPlan]
DP_Exception=
DP_Rule1=
DP_Rule2=

[Advanced]
CellNumber=
Transport=
isAccountActive=1

[Device]
VirtualMAC=
ToolReset=
Mode=3
EOD;


$message =  array(
"Request" =>"AddUpdateBaseINI",
"GMT_Timestamp"=>$timestamp,
"SessionID"=>$session_id,
"tenant_id" =>"1",
"INI"=>"$string"
);

$request = json_encode($message);
echo $request;
echo "<pre>";
print_r(json_decode($request,true));

答案 1 :(得分:0)

工作正常

    <?php

     $message =  array(
    "Request" =>"AddUpdateBaseINI",
    "GMT_Timestamp"=>$timestamp,
    "SessionID"=>$session_id
    "tenant_id" =>"1",
    "INI"=>"[PRODUCT]
    Username=9008
    Password=45645646464654

    [DialPlan]
    DP_Exception=
    DP_Rule1=
    DP_Rule2=

    [Advanced]
    CellNumber=
    Transport=
    isAccountActive=1

    [Device]
    VirtualMAC=
    ToolReset=
    Mode=3"

    );

    echo json_encode($message);
    echo"<pre>";
    print_r(json_decode(json_encode($message),true));

https://eval.in/777403