Amy
Amy

Reputation: 133

How to decode a json correctly in php?

I wrote this element but when I want to display the json, it does'nt work ? I have a null result my complete code

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

result on 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": "" } ] } "

result on var_dump($result_json) is NULL

result on 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": "" } ] } 

result on print_r($result_json) is blank

the json on the server is like that

{
  "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":    ""
    }
  ]
}

Upvotes: 1

Views: 54

Answers (1)

mujtaba_ahmad
mujtaba_ahmad

Reputation: 402

Maybe you need to investigate how exactly the json data is formatted at source. json_decode does not allow single quotes, unquoted keys/names and trailing commas in the json data.

You might also need to check if doing json_decode($json, true); solves your problem

EDIT :

I see

Looks like there is an error in your json at line 13 "apps_name": "\BestSelling",. You need to escape the wild backslash in "\BestSelling". The valid json fragment would be "apps_name": "\\BestSelling",.

You need to replace any single backslashes with \\ in your json data before calling json_decode

Upvotes: 1

Related Questions