Jump to content

Read JSON instead of XML, need some HELP


danjapro
Go to solution Solved by requinix,

Recommended Posts

I need some Help, I need to re-write my code slightly to read JSON files with Key:Value that there for VML.

 

I just need to read json files instead of reading XML files.

Can someone assist, I think it i simple adjustment, I am having hard time.

 


 /**
     * Function install
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
 
        $reflector = new \ReflectionClass('Incomm\Cms\Setup\UpgradeData');
        $reflector->getFileName();
        $contentDirectory = dirname($reflector->getFilename()) . "/../Content/";
 
        $cmsBlocksFiles = glob("{$contentDirectory}Blocks/*xml");
        $cmsPagesFiles = glob("{$contentDirectory}Pages/*xml");
        $homePageFiles = glob("{$contentDirectory}Homepage/*xml");
 
        /* CMS Blocks */
        foreach ($cmsBlocksFiles as $file) {
            $xml = simplexml_load_file($file, "SimpleXMLElement", LIBXML_NOCDATA);
            $json = json_encode($xml);
            $data = json_decode($json, TRUE);
            unset($data["version"]);
            $tmpBlock = $this->blockFactory->create(['data' => $data]);
            $this->blockRepository->save($tmpBlock);
        }
 
        /* CMS Pages */
        foreach ($cmsPagesFiles as $file) {
            $xml = simplexml_load_file($file, "SimpleXMLElement", LIBXML_NOCDATA);
            $json = json_encode($xml);
            $data = json_decode($json, TRUE);
            $page = $this->pageFactory->create();
            $page->setTitle($data['title'])
                ->setIdentifier($data['identifier'])
                ->setIsActive(true)
                ->setPageLayout($data['page_layout'])
                ->setStores(array(0))
                ->setContent($data['content'])
                ->save();
        }
 
        /* Home page */
        $newPage = $this->pageFactory->create()->load(
            'home',
            'identifier'
        );
 
        if ($newPage->getId()) {
            foreach ($homePageFiles as $file) {
                $xml = simplexml_load_file($file, "SimpleXMLElement", LIBXML_NOCDATA);
                $json = json_encode($xml);
                $data = json_decode($json, TRUE);
                $newPage->setContent($data["content"]);
                $newPage->save();
            }
        }
 
        $setup->endSetup();
    }
}
 

Link to comment
Share on other sites

  • Solution

1. Fix those three lines near the top so they find the right files.

2. Replace the three stupid pairs of simplexml_load_file/json_encode lines with one that uses file_get_contents to load the JSON data into $json. Keep the json_decode lines.

3. There is no third step.

 

That's assuming the JSON structure mirrors the XML structure. If it does not then you have to adjust the code accordingly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.