We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26903
    • 1,336 Posts
    Question on encoding/decoding json data,

    From the getnodes.php processor we have this code :-

    return $this->toJSON($items);
    where $items is the array of nodes for the resource tree.

    How do I decode this back into an array in PHP? I’ve tried $modx->fromJSON in various combinations with the incoming json string and the encode/decode literal functions in the modx->response object but to no avail. The resulting array is always null, presumably this string is decoded by the js JSONreader, so do we have a php equivalent of this. I’m sure this is easy and I’ve missed something here.

      Use MODx, or the cat gets it!
      • 28215
      • 4,149 Posts
      Quote from: shamblett at Jun 26, 2009, 04:55 AM

      How do I decode this back into an array in PHP? I’ve tried $modx->fromJSON in various combinations with the incoming json string and the encode/decode literal functions in the modx->response object but to no avail. The resulting array is always null, presumably this string is decoded by the js JSONreader, so do we have a php equivalent of this. I’m sure this is easy and I’ve missed something here.

      $modx->fromJSON($string) should work. It might, however, be tripping on the literals (this, function, new, etc). I’d have to understand more of what you’re trying to do to help.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 26903
        • 1,336 Posts
        Yes sorry, should have said what I’m trying to achieve here. I have a JSON response string as encoded by the getnodes.php processor, a sample from the start is :-

        [{"text":"Page 3 (2)","id":"alt_2","pk":2,"leaf":1,"cls":"icon-document","type":"modResource","qtip"
        :"<i><\/i>","preview_url":"http:\/\/10.92.20.228\/revolution\/index.php?id=2","href":"?a=40&id=2","menu"
        :{"items":[{"id":"cm-resource-header","text":"<b>Page 3<\/b> <i>(2)<\/i>","params":"","handler":function
        () { return false; },"header":true},"-",{"id":"cm-resource-view","text":"View Resource","handler":function
        () {
        this.loadAction("a=40");..........etc etc

        If returned straight to my provisioner resource tree panel, which is derived from MODx.tree.Tree it displays fine, layout, names, qtips, menus etc all Ok.

        What I want to do before I return it is to take off the menus it comes with and add my own, so I want to convert this string back to a PHP array, add my menus and re-encode it, then pass it back. I can see the re-encoding as the response object has the toJSON method, its the decoding that seems to break. the XPDO fromJSON just returns a null array, as it does if I do a decode_literals on it first(or encode_literals for that matter).

        If you only do this in javascript maybe there isn’t a PHP equivalent yet in the code, I can’t seem to find one.

        I could of course mung the JSON string but this is very kludgy. I’d rather dig into this by using a simple PHP script that instantiates the Services_JSON class from the json/JSON.php file and step through it to find out what’s occurring in the decode if needs be.




          Use MODx, or the cat gets it!
          • 28215
          • 4,149 Posts
          Quote from: shamblett at Jun 26, 2009, 08:07 AM

          What I want to do before I return it is to take off the menus it comes with and add my own, so I want to convert this string back to a PHP array, add my menus and re-encode it, then pass it back. I can see the re-encoding as the response object has the toJSON method, its the decoding that seems to break. the XPDO fromJSON just returns a null array, as it does if I do a decode_literals on it first(or encode_literals for that matter).
          So you’re returning the data from getnodes, then sending it back to another processor? Or are you just including the processor in your own processor?

          I’m still confused as to your methods of how you’re doing this.
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 26903
            • 1,336 Posts
            OK, extract from the code, this is in my provisioner class, called as usual from my local processor for the resources tab :-

            function getResources($id, $type, &$errorstring, &$nodes)
            	{
            		
            		$changedmenus = array();
            		
            		// Logged in check
            		if ( !$this->_loggedin ) {
            			
            			$error = $this->modx->lexicon('notloggedin');
            			return false;
            		}
            		
            		// get the resources
            		curl_setopt($this->_curlSession, CURLOPT_URL, $this->_connectorURL."/resource/index.php?action=getnodes&id=$id&type=$type");
            		
            		$result = curl_exec($this->_curlSession);
            


            The $result contains the JSON encoded string from the remote getnodes processor, if I return this as is the tree draws Ok. If I do this :-

            $resultarray = $this->modx->fromJSON($result);


            I get a null array.

            I’ve also tried

            $result = $this->modx->response->_decodeLiterals($result)
            before the JSON call, no luck.





              Use MODx, or the cat gets it!
              • 28215
              • 4,149 Posts
              Hmm, not sure. But that makes a valid point - there should be a toggle REQUEST param available to you so that you can specify the return type, JSON or array. I’ll see what I can whip up.
                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 26903
                • 1,336 Posts
                OK, the getnodes processor doesn’t support this, it just returns JSON. I’m happy with this behaviour as it happens(we want it to be in JSON don’t we as its coming over the net from a remote site(nor sure here.) I was just a bit baffled as to why the fromJSON call didn’t work. I’ll find out and post back.

                Edit - or maybe we can add the toggle to encode in another format but there’s more work here.
                  Use MODx, or the cat gets it!
                  • 28215
                  • 4,149 Posts
                  Quote from: shamblett at Jun 26, 2009, 09:02 AM

                  Edit - or maybe we can add the toggle to encode in another format but there’s more work here.
                  I can probably add a toggle to encode the literals as $this->toJSON does ($modx->toJSON does not). That way you should be able to reverse-parse it.

                  P.S. By the way, we’re on #modx and #xpdo in irc.freenode.net if you want a bit quicker conversation. We’re encouraging our devs to go there, since we want to develop the dev community.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 26903
                    • 1,336 Posts
                    P.S. By the way, we’re on #modx and #xpdo in irc.freenode.net if you want a bit quicker conversation. We’re encouraging our devs to go there, since we want to develop the dev community.

                    Yes, no probs, will do

                      Use MODx, or the cat gets it!
                      • 28215
                      • 4,149 Posts
                      FYI:

                      SVN commit revision 5242:
                      - Added parameter to the getNodes processor for resources/elements called ’stringLiterals’ which, when true, does not encode the JS literals
                        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com