如何在php中正确解码json?

问题描述:

我写了这个元素,但是当我想显示json时,它不起作用? 我有一个空的结果 我的完整代码如何在php中正确解码json?

 $dir = OSCOM::getConfig('dir_root', 'Shop') . $this->ModuleInfosJson . '/'; 

     $result = glob($dir . '*' . $module_name . '*.json'); 


     if (!empty($result)) { 
     $delete_path = str_replace($dir, '', $result); 
     $delete_extansion = str_replace('.json', '', $delete_path); 
     $filename = $delete_extansion; 

     foreach ($filename as $module) { 
      if (is_file($dir . $module . '.json')) { 
      $json = @file_get_contents(OSCOM::getConfig('dir_root', 'Shop') . $this->ModuleInfosJson . '/' . $module . '.json', true, $this->context); 

var_dump($json); 

      $result_json = json_decode($json); 

var_dump($result_json); 
} 

结果上的var_dump($ result_json);

"{ "title": "module_apps_best_selling", "type": "apps", "vendor": "", "is_free": "Yes", "website_to_sell" : "", "version": 1.0, "req_core_version": 3.0, "license": "GPL 2", "tag": "apps, module, products, listing, best selling", "install": "includes/Apps/", "module_directory": "ProductsListing", "apps_name": "\BestSelling", "type_module": "fixe", "dependance": "", "description": "This module allow you to insert a new page called Best Selling inside the catalog. It create also in the template directory a new directory where you can manage and create template.", "image": "ModuleInfosJson/apps_best_selling.png", "authors": [ { "name": "", "company": "", "email": "", "website": "", "Community": "" } ] } " 

var_dump($result_json)结果是对print_r($result_json)print_r($json)

{ "title": "module_apps_best_selling", "type": "apps", "vendor": "", "is_free": "Yes", "website_to_sell" : "", "version": 1.0, "req_core_version": 3.0, "license": "GPL 2", "tag": "apps, module, products, listing, best selling", "install": "includes/Apps/", "module_directory": "ProductsListing", "apps_name": "\BestSelling", "type_module": "fixe", "dependance": "", "description": "This module allow you to insert a new page called Best Selling inside the catalog. It create also in the template directory a new directory where you can manage and create template.", "image": "ModuleInfosJson/apps_best_selling.png", "authors": [ { "name": "", "company": "", "email": "", "website": "", "Community": "" } ] } 

结果NULL

结果是空白

服务器上的JSON就是这样

{ 
    "title":   "module_apps_best_selling", 
    "type":  "apps", 
    "vendor":   "", 
    "is_free": "Yes", 
    "website_to_sell" : "", 
    "version":   1.0, 
    "req_core_version": 3.0, 
    "license":   "GPL 2", 
    "tag":    "apps, module, products, listing, best selling", 
    "install":   "includes/Apps/", 
    "module_directory": "ProductsListing", 
    "apps_name":   "\BestSelling", 
    "type_module":  "fixe", 
    "dependance":   "", 

"description":  "This module allow you to insert a new page called Best Selling inside the catalog. It create also in the template directory a new directory where you can manage and create template.", 
    "image":    "ModuleInfosJson/apps_best_selling.png", 

    "authors": [ 
    { 
     "name":   "", 
     "company":  "", 
     "email":   "", 
     "website":  "", 
     "Community": "" 
    } 
    ] 
} 
+0

+0

看起来像已经编码尝试print_r($ json)而不是var_dump – Marcin

+1

它看起来像你的json是无效的。你能发布完整的字符串吗? – jeroen

也许您需要调查json数据在源文件中的格式。 json_decode不允许单引号,未加引号的键/名称和json数据中的尾随逗号。

您可能还需要检查,如果这样做 json_decode($json, true);解决您的问题

编辑:

我看到

看起来有在第13行"apps_name": "\BestSelling",在JSON的错误。您需要跳过"\BestSelling"中的野生反斜杠。 有效的json片段将是"apps_name": "\\BestSelling",

你需要调用json_decode之前,在你的JSON数据\\更换任何单反斜线

+1

添加'true'应该无关紧要,因为如果数据有效,它仍然会将数据作为对象输出。 –

+0

用更多解释更改我的代码 – Amy

+0

@Amy我用解决方案更新了答案 –