且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将类stdClass的Object转换为JSON Object

更新时间:2023-02-18 22:31:55

$ data = json_encode((array)$会话); 是正确的方法。

我猜你在那里展示的HTML是你在firebug中看到的/ chrome dev工具/ IE开发工具。这就是浏览器尝试渲染它之后的样子。尝试查看原始源以查看输出实际上是什么样的。此时您的问题是由双引号引起的 json_encode 导致浏览器引擎在结束值之前关闭 onclick 属性$ data

I'm guessing that the html you are showing there is what you see in firebug/chrome dev tools/IE dev tools. That is what it looks like after the browser attempts to render it. Try looking at the raw source to see what the output actually looks like. Your problem at this point is caused by the double quotes from json_encode causing the browser engine to consider the onclick attribute closed before the end of the values from $data.

您需要在HTML中混合使用双引号和/或使用单引号。我从单引号开始,因为这会更简单。但是你还必须确保 $ data 中没有任何值包含未转义的单引号(因为这会导致同样的问题)。

You will need to do some mix of escaping double-quotes and/or using single-quotes in the HTML. I'd start with single-quotes, as that will be more simple. But you will also have to make sure none of the values in $data include unescaped single quotes (as that would result in the same problem).