php 将类 stdClass 的对象转换为 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16062824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:27:56  来源:igfitidea点击:

Convert Object of class stdClass to JSON Object

phpjavascriptjsonobjectconverter

提问by nielsv

this is what i have in my php file:

这是我的 php 文件中的内容:

$session = $m->session;

$session is now:

$session 现在是:

object(stdClass)[31]
  public 'id' => string '21112' (length=5)
  public 'external_id' => string '' (length=0)
  public 'sessiongroupid' => string '1843' (length=4)
  public 'eventid' => string '5588' (length=4)
  public 'order' => string '0' (length=1)
  public 'name' => string 'Ferdau Conference' (length=17)
  public 'description' => string 'Ferdau Conference' (length=17)
  public 'starttime' => string '2013-04-18 18:00:00' (length=19)
  public 'endtime' => string '2013-04-18 18:04:00' (length=19)
  public 'speaker' => string '' (length=0)
  public 'location' => string 'Waregem' (length=7)
  public 'mapid' => string '0' (length=1)
  public 'xpos' => string '0.000000' (length=8)
  public 'ypos' => string '0.000000' (length=8)
  public 'maptype' => string 'plan' (length=4)
  public 'imageurl' => string '' (length=0)
  public 'presentation' => string '' (length=0)
  public 'organizer' => string '0' (length=1)
  public 'twitter' => string '' (length=0)
  public 'allowAddToFavorites' => string '0' (length=1)
  public 'allowAddToAgenda' => string '0' (length=1)
  public 'votes' => string '0' (length=1)
  public 'url' => string 'http://ferdau.be' (length=16)
  public 'venueid' => string '0' (length=1)

I want to send all the information to a function named save in my javascript file. Like this:

我想将所有信息发送到我的 javascript 文件中名为 save 的函数。像这样:

echo '<a onclick="save('.$session.')" style="cursor:pointer;" class="metacell">
                        <img src="'.buildUri("images/icons/favorite.png").'" width="16" />
                        <span>Add to favorites</span>
                    </a>';

When I try this I always get an error that only a string can be send. How can I convert this object to something I can send to my javascript function?

当我尝试这个时,我总是收到一个错误,即只能发送一个字符串。如何将此对象转换为可以发送到我的 javascript 函数的内容?

I've tried this with no result: $data = json_encode($session);

我试过没有结果: $data = json_encode($session);

When I do $data = json_encode((array)$session)

当我做 $data = json_encode((array)$session)

I get this:

我明白了:

<a class="metacell" style="cursor:pointer;" ferdau.be","venueid":"0"})"="" \="" 18:04:00","speaker":"","location":"waregem","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowaddtofavorites":"0","allowaddtoagenda":"0","votes":"0","url":"http:\="" 18:00:00","endtime":"2013-04-18="" conference","starttime":"2013-04-18="" conference","description":"ferdau="" id":"21112","external_id":"","sessiongroupid":"1843","eventid":"5588","order":"0","name":"ferdau="" onclick="save({">

回答by Jeffrey Blake

$data = json_encode((array)$session);is the right way to go.

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

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_encodecausing the browser engine to consider the onclickattribute closed before the end of the values from $data.

我猜你在那里展示的 html 就是你在 firebug/chrome 开发工具/IE 开发工具中看到的。这就是浏览器尝试呈现它后的样子。尝试查看原始源以查看输出的实际情况。您此时的问题是由双引号json_encode引起的,导致浏览器引擎认为onclick属性在$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 $datainclude unescaped single quotes (as that would result in the same problem).

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