【发布时间】:2011-02-20 22:27:15
【问题描述】:
我想在php中这样写。我怎样才能同样表达成php?
$test = '{"longUrl": "http://www.yahoo.com"}';
谢谢。
【问题讨论】:
-
我想在代码中用作字符串。它不包含 json 或数组等。因此,它会给出这样的字符串错误。
标签: php syntax syntax-error
我想在php中这样写。我怎样才能同样表达成php?
$test = '{"longUrl": "http://www.yahoo.com"}';
谢谢。
【问题讨论】:
标签: php syntax syntax-error
如果您想编写实际的 PHP 代码来创建一个新对象(假设您的示例是 JSON),那么 PHP 中没有文字/快捷语法。您必须创建一个新的 stdClass 对象并手动设置其变量:
$test = new stdClass;
$test->longUrl = "http://www.yahoo.com";
如果您喜欢在字符串中编写 JSON,就像您在示例中所做的那样,只需将其输入到 json_decode() 中,您就会拥有一个 stdClass 对象:
$test = json_decode('{"longUrl": "http://www.yahoo.com"}');
【讨论】:
$test = array("longUrl" => "http://www.yahoo.com");
>echo $test['longUrl']
http://www.yahoo.com
【讨论】: