0

I'm trying to return array from this json

 $jsondata = "{ 'map' : {'center': {'lat': 24.68599, 'lng': 46.7026558}, 
 'zoom': 16}, 'marker': { 'position': {'lat': 24.68599, 'lng': 46.7026558 }, 
 'icon': '<?php bloginfo('stylesheet_directory'); ? 
 >/resources/images/temp/marker.png' } }";

$result = json_decode($jsondata, true);

The result is always null. Can someone tell me why and how to fix it? I suppose single quotes have to be replaced with double but that didn't work anyway.

Svetlozar
  • 967
  • 2
  • 10
  • 23
  • Possible duplicate of [PHP JSON parsing giving an error](https://stackoverflow.com/questions/3709705/php-json-parsing-giving-an-error) – Mike Doe Dec 08 '18 at 13:59
  • 1
    Possible duplicate of [PHP json\_decode() returns NULL with valid JSON?](https://stackoverflow.com/questions/2410342/php-json-decode-returns-null-with-valid-json) – Darragh Enright Dec 08 '18 at 14:01
  • Have a look at [`json_last_error()`](http://php.net/manual/en/function.json-last-error.php) and [`json_last_error_msg()`](http://php.net/manual/en/function.json-last-error-msg.php) in the PHP docs – Darragh Enright Dec 08 '18 at 14:02
  • Your input string is not [JSON](http://json.org). – axiac Dec 08 '18 at 14:03

2 Answers2

2

You are using single quotes ' istead of double quotes " for keys.

$jsondata = '{ "map" : {"center": [...]

Furthermore there are issues with the "bloginfo" part.

The code as you wrote it doesn't make sense.

Assuming bloginfo is a function that returns a string that represents a path and that you can invoke directly from the current scope then you can call it, append the final part of the path and finally JSON encode the string to properly escape every character.

This way: json_encode( bloginfo("stylesheet_directory") . '/resources/images/temp/marker.png' )

The whole thing becomes:

 $jsondata = '{ "map" : {"center": {"lat": 24.68599, "lng": 46.7026558}, 
 "zoom": 16}, "marker": { "position": {"lat": 24.68599, "lng": 46.7026558 }, 
 "icon": '. json_econde( bloginfo("stylesheet_directory")  . 
 '/resources/images/temp/marker.png' ). ' } }';
Paolo
  • 15,233
  • 27
  • 70
  • 91
  • I've tried to replace single quotes with double and double quotes in the begining with single. The result is still null – Svetlozar Dec 08 '18 at 13:57
  • The problem is this section: `' – R. García Dec 08 '18 at 14:02
  • @R.García yes, I agree. I completed the answer – Paolo Dec 08 '18 at 14:07
  • @NigelRen no, I don't need to. `json_encode` does that as a string is passed as argument: `json_encode( 'hello' ) ====> "hello"` – Paolo Dec 08 '18 at 14:10
  • @Paolo But in my opinion the solution that you are proposing it's difficult without needed. Maybe we could improve your solution with **Escape Sequences:** https://phppot.com/php/php-escape-sequences/ – R. García Dec 08 '18 at 14:12
1

When you are building the JSON, the following bit won't run the PHP code your expecting to ...

 'icon': '<?php bloginfo('stylesheet_directory'); ? 
 >/resources/images/temp/marker.png' } }";

If you tried to output the final JSON string you would probably see it isn't what your expecting. Instead you should build the string in parts...

 'icon': '". bloginfo('stylesheet_directory'). 
 "/resources/images/temp/marker.png' } }";

You also need to change it to use double quotes as well as pointed out in the other answer. So you need to do BOTH things...

$jsondata = '{ "map" : {"center": {"lat": 24.68599, "lng": 46.7026558},
 "zoom": 16}, "marker": { "position": {"lat": 24.68599, "lng": 46.7026558 },
 "icon": "'.bloginfo('stylesheet_directory').'/resources/images/temp/marker.png" } }';
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55