JSON,字符编码或无法识别的文件格式不正确

时间:2016-03-08 14:52:15

标签: php json magento serialization character-encoding

我正在使用Magento,这个特殊的值应该是我能解析的有效json。看起来字符编码出了问题。我得到了这个。

a:10:{s:4:"type";s:10:"image/jpeg";s:5:"title";s:25:"the-goose-will-attack.jpg";s:10:"quote_path";s:68:"/media/custom_options/quote/t/h/94b0992d58b4478d13a2376f645d3b7c.jpg";s:10:"order_path";s:68:"/media/custom_options/order/t/h/94b0992d58b4478d13a2376f645d3b7c.jpg";s:8:"fullpath";s:102:"/var/www/mageprodcory/mage-webroot/media/custom_options/quote/t/h/94b0992d58b4478d13a2376f645d3b7c.jpg";s:4:"size";s:5:"42421";s:5:"width";i:450;s:6:"height";i:600;s:10:"secret_key";s:20:"94b0992d58b4478d13a2";s:3:"url";a:2:{s:5:"route";s:35:"sales/download/downloadCustomOption";s:6:"params";a:2:{s:2:"id";s:3:"133";s:3:"key";s:20:"94b0992d58b4478d13a2";}}}

它几乎看起来像有效的json,但;s:25:类型的东西并不属于。它应该是json,但我想它也可能是其他文件格式。

我无法在;s:25:类型字符上找到任何字符编码信息,主要是因为它很难搜索。我试过禁用魔法引号,但这不是原因。

知道为什么JSON不正确,或者这个文本可能是什么文件格式,以及我如何在PHP中解析它?

1 个答案:

答案 0 :(得分:1)

这个字符串不是json。它是序列化的价值。运行以下代码:

<?php

var_dump(unserialize('a:10:{s:4:"type";s:10:"image/jpeg";s:5:"title";s:25:"the-goose-will-attack.jpg";s:10:"quote_path";s:68:"/media/custom_options/quote/t/h/94b0992d58b4478d13a2376f645d3b7c.jpg";s:10:"order_path";s:68:"/media/custom_options/order/t/h/94b0992d58b4478d13a2376f645d3b7c.jpg";s:8:"fullpath";s:102:"/var/www/mageprodcory/mage-webroot/media/custom_options/quote/t/h/94b0992d58b4478d13a2376f645d3b7c.jpg";s:4:"size";s:5:"42421";s:5:"width";i:450;s:6:"height";i:600;s:10:"secret_key";s:20:"94b0992d58b4478d13a2";s:3:"url";a:2:{s:5:"route";s:35:"sales/download/downloadCustomOption";s:6:"params";a:2:{s:2:"id";s:3:"133";s:3:"key";s:20:"94b0992d58b4478d13a2";}}}'));

输出将是这样的:

array(10) {
  ["type"]=>
  string(10) "image/jpeg"
  ["title"]=>
  string(25) "the-goose-will-attack.jpg"
  ["quote_path"]=>
  string(68) "/media/custom_options/quote/t/h/94b0992d58b4478d13a2376f645d3b7c.jpg"
  ["order_path"]=>
  string(68) "/media/custom_options/order/t/h/94b0992d58b4478d13a2376f645d3b7c.jpg"
  ["fullpath"]=>
  string(102) "/var/www/mageprodcory/mage-webroot/media/custom_options/quote/t/h/94b0992d58b4478d13a2376f645d3b7c.jpg"
  ["size"]=>
  string(5) "42421"
  ["width"]=>
  int(450)
  ["height"]=>
  int(600)
  ["secret_key"]=>
  string(20) "94b0992d58b4478d13a2"
  ["url"]=>
  array(2) {
    ["route"]=>
    string(35) "sales/download/downloadCustomOption"
    ["params"]=>
    array(2) {
      ["id"]=>
      string(3) "133"
      ["key"]=>
      string(20) "94b0992d58b4478d13a2"
    }
  }
}