简体   繁体   English

PHP 将 JSON 字符串转换为对象或数组

[英]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.我知道如果我在 google 上搜索这个,你会得到很多点击,过去 2 天我一直在试图弄清楚发生了什么,但没有成功。

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.所以,我正在使用 Wordpress,但不确定这与我的问题有任何相关性,并且我在存储中有一个有效的 JSON 字符串,我需要将它放入 PHP 然后放入一个对象。

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: JSON 字符串是 PHP 中的一个对象,我将其转换为因为我在 sessionStorage 中需要它,所以这里是我在 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:这是现在存储在浏览器 sessionStorage 中的对象:

{"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.这已通过 JSONLint 验证。

Here is the PHP code:这是PHP代码:

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

If i now print_r($time) I get the same printed, a valid JSON string.如果我现在print_r($time)我得到同样的打印,一个有效的 JSON 字符串。 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根据我读过的内容,PHP 有json_decode来满足这样的要求,但它不起作用,但它确实给了我一个空格

$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.如果我尝试使用json_encode它会给我有趣的角色。

If I try and make it an object using :如果我尝试使用以下方法使其成为对象:

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

It gives me :它给了我:

stdClass Object ( ) stdClass 对象 ( )

$time = (object) "document.write(time)"; $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 stdClass 对象 ( [scalar] => {"uuid":"1788-417f-b5e6-970bab44ec37","low":2629,"run":5046793,"contact":"a0eb0-4977-ad10-af7aecf4ef80","完成":true,"values":[{"category":{"base":"Great","eng":"Great"},"node":"f85ed85-495e-8eaf-1e6cfdbadf97","time" :"2015-10-12T11:28:13.371Z","text":"非常感谢","rule_value":"太棒了","value":"太棒了","label":"响应 1"}], "步骤":[{"节点":"83e332e0-a45-a4f6-cee688f9395b","arrived_on":"2015-10-12T11:27:43.866Z","left_on":"2015-10-12T11 44.052Z","text":"你好!早上好?","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":"非常感谢","type":"R","value ":"太棒了"},{"node":"424912da-e680-432d-877c-307117366f98","arrived_on":"2015-10-12T11:28:13.374Z","left_on":"2015-1 12T11:28:13.477Z","text":"太好听了!祝你有美好的一天","type":"A","value":"None"}],"created_on":"2015 -10-12T11:27:43.721Z","modified_on":"2015-10-12T11:28:13.482Z","过期s_on":null,"expired_on":null,"runtime":"12 - 10 @ 11 hour"} ) s_on":null,"expired_on":null,"runtime":"12 - 10 @ 11 小时"})

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.我可以选择使用 Javascript 并使用这些信息,但我真的需要在 PHP 中使用它,因为还有更多工作需要处理这些信息。

Could someone please help point me where, and why, it's going wrong ?有人可以帮我指出哪里出错了,为什么出错?

You can't get a javascript variable value using PHP code.您无法使用 PHP 代码获取 javascript 变量值。 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.如果你想在后端使用这些数据,你需要创建一个 AJAX 请求,正如 RambRaider 所说。 If you need this in the front-end, you can simply parse the json data using JSON.parse .如果你在前端需要这个,你可以简单地使用JSON.parse解析 json 数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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