【问题标题】:How to send [ ] as string in JSON?如何在 JSON 中将 [ ] 作为字符串发送?
【发布时间】:2017-04-17 05:37:20
【问题描述】:

在特定情况下,我想将 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"} ) )

【问题讨论】:

  • 预期输出是什么?
  • 请对此How Do I pass "[ ]" as string and not json object进行更多解释?
  • 我已经添加了预期的输出@JYoThI
  • 它在解码时正确编码,并按预期输出为字符串。 "[PRODUCT] Username=9008 Password=45645646464654 [DialPlan] DP_Exception= DP_Rule1= DP_Rule2=[Advanced] CellNumber=Transport=isAccountActive=1[Device]VirtualMAC=ToolReset=Mode=3" 它不认为是数组。
  • 您的预期输出不正确,因为换行符被空格替换。但是[] 并没有什么特别之处。

标签: php json codeigniter


【解决方案1】:

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

$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));

【讨论】:

  • 不需要&lt;&lt;&lt;EOD我们可以正常对字符串进行编码和解码
  • @NishantNair OP 想要在JSON 中传递INI file 数据,所以他需要将它作为字符串传递。所以它是传递多行字符串的最佳方法。
  • 不需要它被正确编码,请检查 cmets。还看看他期望的响应与我的答案链接相同
  • @NishantNair Heredoc 文本的行为就像一个双引号字符串,没有双引号。这意味着heredoc中的引号不需要转义,但仍然可以使用上面列出的转义码。
【解决方案2】:

工作正常

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

【讨论】:

  • 我使用 CURL 请求将这个发送到服务器我如何解码相同的数据?
  • 你到底想从 json 中得到什么?它被指定的数组完美地解析和编码
  • 好的,我知道它的编码正确但是我如何解码相同的数据?你能给我看一些示例代码吗?当我在服务器上使用 CURL 时,我在 $_POST[] 中有 json 数据,现在我想解码 $_POST[]
  • 是不是你正在点击 curl 请求并且你在 json 中得到响应,现在你需要在你身边解码它
  • 总是打印 curl 的 $result = curl_exec($ch); 结果。因此,您可以检查服务器的响应是什么。取决于你可以解码它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 2020-11-11
  • 2018-06-27
  • 2022-11-29
相关资源
最近更新 更多