简体   繁体   中英

PHP converting JSON string into Object or Array

I know if I search this on google you get lots of hits back, and i have spent the last 2 days trying to figure out whats happening, and been unsuccessful.

So, I am using Wordpress, but not sure that has any relevance to my issue, and I have a valid JSON string in storage and I need it to be put in PHP and then an object.

The JSON string was an object in PHP which i converted because i needed it in sessionStorage, so here is how i converted it in PHP:

$encodedTime = json_encode($time);
$encodedTime = str_replace("'", "", $encodedTime);

This is then loaded into a element:

<a class="button" href="/?page_id=714" name="viewButton" rel='<?php echo $encodedTime ?>'>View</a>

Here is the object now stored into sessionStorage on the browser:

{"uuid":"108-417f-b5e6-970baec37","low":2929,"run":50793,"contact":"a07-e4b0-4977-ad10-af7aecf4ef80","completed":true,"values":[{"category":{"base":"Great","eng":"Great"},"node":"f85ed85-495e-8eaf-1e6cfdbadf97","time":"2015-10-12T11:28:13.371Z","text":"Great thanks","rule_value":"Great","value":"Great","label":"Response 1"}],"steps":[{"node":"83e332e0-a456-449f-a4f6-cee688f9395b","arrived_on":"2015-10-12T11:27:43.866Z","left_on":"2015-10-12T11:27:44.052Z","text":"Hello! Good Morning  This morning?","type":"A","value":"None"},{"node":"f85ed8a8-e745-495e-8eaf-1e6cfdbadf97","arrived_on":"2015-10-12T11:27:44.052Z","left_on":"2015-10-12T11:28:13.371Z","text":"Great thanks","type":"R","value":"Great"},{"node":"424912da-e680-432d-877c-307117366f98","arrived_on":"2015-10-12T11:28:13.374Z","left_on":"2015-10-12T11:28:13.477Z","text":"Thats great to hear!, Wishing you a good day","type":"A","value":"None"}],"created_on":"2015-10-12T11:27:43.721Z","modified_on":"2015-10-12T11:28:13.482Z","expires_on":null,"expired_on":null,"runtime":"12 - 10 @ 11 hour"}

This has been validated with JSONLint.

Here is the PHP code:

$time = "<script>document.write(time)</script>";

If i now print_r($time) I get the same printed, a valid JSON string. Great!

Based on what i have read PHP has json_decode for such a requirement, but it does not work, but it does give me a blank space

$time = json_decode("<script>document.write(time)</script>", true); // PHP Code
print_r($time);

If I try and json_encode it gives me funny characters.

If I try and make it an object using :

$time = (object) json_decode("<script>document.write(time)</script>", true);

It gives me :

stdClass Object ( )

$time = (object) "document.write(time)";

stdClass Object ( [scalar] => {"uuid":"1788-417f-b5e6-970bab44ec37","low":2629,"run":5046793,"contact":"a0eb0-4977-ad10-af7aecf4ef80","completed":true,"values":[{"category":{"base":"Great","eng":"Great"},"node":"f85ed85-495e-8eaf-1e6cfdbadf97","time":"2015-10-12T11:28:13.371Z","text":"Great thanks","rule_value":"Great","value":"Great","label":"Response 1"}],"steps":[{"node":"83e332e0-a45-a4f6-cee688f9395b","arrived_on":"2015-10-12T11:27:43.866Z","left_on":"2015-10-12T11:27:44.052Z","text":"Hello! morning?","type":"A","value":"None"},{"node":"f85ed8a8-5e-8eaf-1e6cfdbadf97","arrived_on":"2015-10-12T11:27:44.052Z","left_on":"2015-10-12T11:28:13.371Z","text":"Great thanks","type":"R","value":"Great"},{"node":"424912da-e680-432d-877c-307117366f98","arrived_on":"2015-10-12T11:28:13.374Z","left_on":"2015-10-12T11:28:13.477Z","text":"Thats great to hear!, Wishing you a good day","type":"A","value":"None"}],"created_on":"2015-10-12T11:27:43.721Z","modified_on":"2015-10-12T11:28:13.482Z","expire s_on":null,"expired_on":null,"runtime":"12 - 10 @ 11 hour"} )

So I am at a loss, as to why it will not work. I have the option to use Javascript and use the information, but I really need it in PHP as there is more working to be done with this information.

Could someone please help point me where, and why, it's going wrong ?

You can't get a javascript variable value using PHP code. They're differente things. If you want to understand more, i recommend you read this question .

So... How to do it?

If you wanna use that data in the back-end, you need to create a AJAX request, as RambRaider said. If you need this in the front-end, you can simply parse the json data using JSON.parse .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM