2012-06-19 95 views
-1

我想保留目標字段的原始值,並使用json_decode使用下面的字符串作爲對象進行解碼:保留字符串的原始值,它像一個JSON

{ 
    "translatorID": "f4a5876a-3e53-40e2-9032-d99a30d7a6fc", 
    "label": "ACL", 
    "creator": "Nathan Schneider", 
    "target": "^https?://(www[.])?aclweb\\.org/anthology-new/[^#]+", 
    "minVersion": "1.0.7", 
    "maxVersion": "", 
    "priority": 100, 
    "browserSupport": "gcs", 
    "inRepository": true, 
    "translatorType": 4, 
    "lastUpdated": "2012-01-01 01:42:16" 
} 
+0

你如何對其進行解碼,碼請。 – Sarfraz

+0

你是如何得到JSON的?它適用於我在這裏:http://codepad.org/io4wUjxc –

+0

我相信一個相同的問題,這是前不久用不同的用戶名問。 (編輯)它似乎已被刪除。 – Brian

回答

1

你可以用json_decode解析之前做的是:

$string = str_replace('\\', '\\\\\\\\', $string); 
var_dump(json_decode($string, true)); 

這必須是json解析器中的一個錯誤。
該方法不是很乾淨,但至少你得到了你的結果。

+0

如果輸入已經逃脫了怎麼辦? – poncha

+0

我根據他的輸入字符串做出了我的回答,用我的替換我得到了正確的結果。 –

+0

他的輸入是有效的json,但是如果他在代碼中輸入** string **值並且不從文件/ db/whatever讀取它,那麼他需要自己加倍反斜槓數量 – poncha

1

您是否試過剝去斜線?

這爲我工作:

$string = '{ 
    "translatorID": "f4a5876a-3e53-40e2-9032-d99a30d7a6fc", 
    "label": "ACL", 
    "creator": "Nathan Schneider", 
    "target": "^https?://(www[.])?aclweb\.org/anthology-new/[^#]+", 
    "minVersion": "1.0.7", 
    "maxVersion": "", 
    "priority": 100, 
    "browserSupport": "gcs", 
    "inRepository": true, 
    "translatorType": 4, 
    "lastUpdated": "2012-01-01 01:42:16" 
}'; 

var_dump(json_decode(stripslashes ($string))); 
+0

這改變了目標的原始值 – PHPst

+0

一個單調乏味的解決方案可能是使用addcslashes和一些正則表達式來轉義由生成的數組/對象上的反斜線未轉義的字符。 –

相關問題