Converting PHP object to JSON object using only Javascript

I am making a mobile app with Phonegap and using WordPress as a backend. I am using Advanced Custom Fields with a Google Maps post field which returns a PHP object to the app using JSON API. My WordPress backend sends a normal JSON object to the app, but inside that object is where a stringified PHP object is returned.

I need to convert the PHP object to a JSON object somehow on the client side(the app which is not in WordPress). I have looked at other answers that say to use json_encode for this but my problem is that the app is just HTML/Javascript and no PHP. Is there a way to use PHP code in the middle of a Javascript function to do this? Or would it be better to change the backend so that it returns a JSON object instead of a PHP object in the first place? If so, how do I do that?

Read More

My experience in PHP is still somewhat limited so any help is appreciated.

edit: To clarify a bit more, I am using WordPress on a separate domain from my Phonegap app and only using the JSON API plugin on the WordPress end. I am then using jQuery Ajax calls to retrieve data from the WordPress backend.

Also the returned PHP object looks like this: a:3:{s:7:"address";s:48:"8915 W 159th St, Orland Hills, IL, United States";s:3:"lat";s:17:"41.60111599999999";s:3:"lng";s:11:"-87.8364575";}

Another way I just thought of as well, would it be possible to just leave it as a PHP object and still read out the values from it somehow? I don’t NEED it to be a JSON array, I just need a way to read the individual elements in the array in one way or another.

Here is also a tiny snippet of the JSON returned to clarify what I’m talking about.

"custom_fields": {
"location": [
"a:3:{s:7:"address";s:48:"8915 W 159th St, Orland Hills, IL, United States";s:3:"lat";s:17:"41.60111599999999";s:3:"lng";s:11:"-87.8364575";}"
]
}

That of course isn’t the entire JSON object but it gives you an idea of what I’m dealing with.

Related posts

3 comments

  1. I know you have a solution that works on the front end, but I still think it’d be better to fix this on the server.

    Based on our conversation in the comments, I’ve had a closer look the code in the WordPress forum. The problem seems to be that the location field is an array of strings, not just a string. maybe_unserialize (and is_serialized, which it uses) don’t handle arrays. Here’s the updated code, which you should be able to drop into your theme’s functions.php. I did a quick test, and it works for me.

    class unserialize_php_arrays_before_sending_json {
        function __construct() {
            add_action( 'json_api_import_wp_post',
                array( $this, 'json_api_import_wp_post' ),
                10,
                2 );
        }
    
        function json_api_import_wp_post( $JSON_API_Post, $wp_post ) {
            foreach ( $JSON_API_Post->custom_fields as $key => $custom_field ) {
                if (is_array($custom_field)) {
                    $unserialized_array = array();
                    foreach($custom_field as $field_key => $field_value) {
                        $unserialized_array[$field_key] = maybe_unserialize( $field_value );
                    }
                    $JSON_API_Post->custom_fields->$key = $unserialized_array;
                }
                else {
                    $JSON_API_Post->custom_fields->$key = maybe_unserialize( $custom_field );
                }
            }
        }
    }
    
    new unserialize_php_arrays_before_sending_json();
    
  2. If you’re using a JSON API to retrieve the data, then why don’t you deliver the data in JSON format to your app? Otherwise you seem to remove much of the point of using an API in the first place… You could of course parse that string in JavaScript if you really want to but that’s a very ugly and error prone solution.

    The JSON API plugin does seem to use JSON:

    https://wordpress.org/plugins/json-api/screenshots/

  3. I need to convert the PHP object to a JSON object somehow on the client side(the app which is not in WordPress).

    This bit here leaves me confused. You do not have PHP objects on the client-side, PHP is a back-end technology. What is returned to the client is a string which can be HTML, XML, JSON, plaintext on any other form of encoding.

    That said, saying you have an object $obj in PHP, you could pass it to your front-end application creating an end-point retrieve_object.php and in there:

    echo json_encode($obj);
    

    So long as that is the only thing your are outputting, you lient-side app can make a request (Eg: AJAX) to retrieve_object.php and get the json object.

    BUT , and this is important (!) in doing so you serialize object properties. You will lose any PHP object method. If any object property is an object itself (EG: A DB Connection) then this will be lost too.

    Hope this helps!

Comments are closed.