1

I am trying to pass JSON object from angular to php like this :

<?php
    $placeBean = "{{masterCtrl.getLastplace();}}";
    //VAR DUMP IS -> string(30) "{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0}"

    $json = '{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0}';
    //VAR DUMP IS -> string(70) "{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0}"
?>

Then I need to parse the json in $placeBean, because it is not yet a json in php, so I use

<?php 
   $manage = json_decode($placeBean);  
?>

And this is the problem... because it doesn't work, in fact the result is an empty value: instead, if I do the same thing with $json( instead of $placeBean), the code works perfectly. I Think the issue is some missing chars, which are placed in$json, and not in $placeBean.

Those are the echo of the variables

ECHO OF JSON ->{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0}

ECHO OF placeBean -> placeBean{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0}

I Also tried to use json_last_error, which gave me this : - Syntax error, malformed

6
  • try to use correct string - the string you are trying to parse isn't a valid json php.net/manual/en/function.json-last-error.php Commented Feb 18, 2018 at 15:05
  • The problem is that the string inside $json, is a simple copy/paste of echo($placeBean ); Commented Feb 18, 2018 at 15:07
  • this is the dump string(32) ""{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0}"" bool(false) Commented Feb 18, 2018 at 15:13
  • Somewhere its adding an extra doublequotes on the string value. What is the exact 'raw' output of php in your browser when you do: echo $placeBean; and echo $json; after those two lines you show at the start of your question? Commented Feb 18, 2018 at 15:15
  • added the two echo Commented Feb 18, 2018 at 15:21

1 Answer 1

0

Well, the first one is not a valid JSON object, for this PHP refuses to parse it.

When you run it though AngularJS it will actually use its own mechanism and replaces the string with the data returned by the masterCtrl.getLastplace() call.

Sign up to request clarification or add additional context in comments.

2 Comments

I am still stucked on this... I understand why the var-dump has different lenght... that is why if I use addcslashes on the two strings, I got for the $json, the correct value, while for the $placeBean , I got this : {{\m\a\s\t\e\r\C\t\r\l.\g\e\t\L\a\s\t\p\l\a\c\e();}}
What I mean is, when you call $json, this function relies on local data and internal logic for transpiling the string with a function call into a valid JavaScript object. The PHP code does not expect this kind of behaviour and does not implement the logic for so. This way, you will not be able to have the PHP code do behave the same as an Angular specific code.

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.