1

If I have an array in JS like this:

{
 "car": "BMW",
 "count": 7 
}

I can generate it from PHP quite easy using this array:

array(
       'car' => 'BMW',
       'count' => 7,
)

and json_encode. This is not a problem.

But what if I have an array in JS like this:

{
   "center": new google.maps.LatLng(-34.397, 150.644),
   "zoom": 8
}

is there also some nice way how to produce it from PHP?

The method above fails because JSON put quotes around "new google.maps.LatLng(-34.397, 150.644)".

2
  • 1
    Your first example isn't an array, it's an object. Commented Jul 31, 2013 at 0:07
  • You are right, sorry for confusion. I'm more PHP than JS ;) Commented Jul 31, 2013 at 0:21

1 Answer 1

2

JSON doesn't support custom types. It's just meant for passing data and to be available to any consumer, whereas google.maps.LatLng() isn't really "data."

So, you'll have to accomplish this in 2 steps:

  1. You can include the values needed in another PHP array for the JSON:

    array(
        'center' => array(
            'lat' => -34.397,
            'lng' => 150.644
        ),
        'zoom' => 8
    )
    
    {
        "center": {
            "lat": -34.397,
            "lng": 150.644
        },
        "zoom": 8
    }
    
  2. Then, once parsed to a JavaScript Object, say data:

    data.center = new google.maps.LatLng(data.center.lat, data.center.lng);
    

    And, if there are numerous examples of such objects in the data, you can specify a reviver function to try to recognize them and create the instances:

    // parse, creating `google.maps.LatLng` from any { lat: ..., lng: ..., ... }
    var data = JSON.parse(jsonString, function (key, value) {
        if (typeof value === 'object' && value.lat && value.lng) {
            return new google.maps.LatLng(value.at, value.lng);
        } else {
            return value;
        }
    });
    

    Example: http://jsfiddle.net/XXbUU/


Side note: JSON and JavaScript make a stronger distinction between "array" types than PHP does.

  • "Associative" arrays in PHP become Objects in JSON and JavaScript.
  • "Non-associative" arrays in PHP become Arrays in JSON and JavaScript.
8
  • 2
    To elaborate on Jonathan's answer, you're trying to store functionality within JSON. JSON is used for structuring data, not executing it. new google.maps.LatLng(-34.397, 150.644) is functionality, and needs a processor to evaluate. JSON is processor-independent because it is just a string. Hence your way around would be to store the coordinates and then instantiate them when in the PHP/JS environments. Commented Jul 31, 2013 at 0:13
  • As another sidenote, this is a better practice than passing an arbitrary function call anyway, since that would open up a security vulnerability. Commented Jul 31, 2013 at 0:16
  • Hmmm, clever :) But not exactly what I want. I want to generate various objects in JS having many properties like new google.maps.LatLng(-34.397, 150.644). Following your way it means that I have to add to JS this second step for any such property. I would like only to add elements to PHP array and not doing this extra step. But I'm afraid that there is no better way than yours :( Anyway thank you for your idea and also for reminding me what is an array/object in JS :) Commented Jul 31, 2013 at 0:19
  • @user2042985 For multiple objects, you can match and replace the Objects with a reviver when parse'ing. See my edit under 2. for an example. Commented Jul 31, 2013 at 0:33
  • 1
    @joequincy Though, the JSON can include clues as to the intended type, such as a "$type" property -- "$type": "LatLng" or "$type": "Marker". Then, if (value && value.$type === 'LatLng') ..., etc. Commented Jul 31, 2013 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.