簡體   English   中英

用PHP中的文字符號/對象初始化程序解釋Javascript對象

[英]Interpreting Javascript objects with literal notation/Object initializers in PHP

我制作了一個PHP腳本,使用curl從遠程網站獲取對象初始化器,對象看起來像這樣

{'403' : {'3' : { qin : 144, qout : 52}, '4' : { qin : 143, qout : 86}, '5' : { qin : 144, qout : 79}, '6' : { qin : 143, qout : 93}, '7' : { qin : 144, qout : 78} }

¿PHP中有一個函數可以解釋這個嗎?還是我需要使用正則表達式?

JSON無效,但是是有效的Javascipt文字表示法。

不要在生產中使用,而是嘗試獲取有效的json輸出

$json = "{'403' : {'3' : { qin : 144, qout : 52}, '4' : { qin : 143, qout : 86}, '5' : { qin : 144, qout : 79}, '6' : { qin : 143, qout : 93}, '7' : { qin : 144, qout : 78} }" ;

$json = preg_replace("/'(\w+)'/", '$1', $json); //remove all single quote
$json = preg_replace("/\s*([a-zA-Z0-9_]+)/", ' "$1"', $json);
$json .= "}"; // Missing End

var_dump($json,json_decode($json));

輸出量

object(stdClass)[1]
  public '403' => 
    object(stdClass)[2]
      public '3' => 
        object(stdClass)[3]
          public 'qin' => string '144' (length=3)
          public 'qout' => string '52' (length=2)
      public '4' => 
        object(stdClass)[4]
          public 'qin' => string '143' (length=3)
          public 'qout' => string '86' (length=2)
      public '5' => 
        object(stdClass)[5]
          public 'qin' => string '144' (length=3)
          public 'qout' => string '79' (length=2)
      public '6' => 
        object(stdClass)[6]
          public 'qin' => string '143' (length=3)
          public 'qout' => string '93' (length=2)
      public '7' => 
        object(stdClass)[7]
          public 'qin' => string '144' (length=3)
          public 'qout' => string '78' (length=2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM