Home  >  Article  >  Backend Development  >  求帮忙看看这段代码哪里出错了

求帮忙看看这段代码哪里出错了

WBOY
WBOYOriginal
2016-06-06 20:25:07915browse

如题,使用php解析json数据,使用下面代码出错:

<code><?php header('Content-Type: text/html; charset=UTF-8');
$json_string= [{"text":"test","created":"Sun Nov 08 01:07:10 +0000 2015","favorite_count":0,"retweet_count":0,"url":"","media_url":""}];
$obj=json_decode($json_string);
echo $obj->text;
?></code>

回复内容:

如题,使用php解析json数据,使用下面代码出错:

<code><?php header('Content-Type: text/html; charset=UTF-8');
$json_string= [{"text":"test","created":"Sun Nov 08 01:07:10 +0000 2015","favorite_count":0,"retweet_count":0,"url":"","media_url":""}];
$obj=json_decode($json_string);
echo $obj->text;
?></code>

求帮忙看看这段代码哪里出错了

  1. 你的如果是字符串, 应该加引号.

  2. 你输出的 text 属性是在 数组里(下标为0)的那个元素里.

或者是下面这样的:

求帮忙看看这段代码哪里出错了

因为不清楚你最初的数据是什么样的, 所以只能猜了.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn