JSON只解码一个php数据
我有JSON数据:JSON只解码一个php数据,php,json,Php,Json,我有JSON数据: [ { "title": "red", }, { "title": "blue", }, { "title": "yellow", }, ] 我只想得到第一个数据,瑞德。 我试着用这个 ... $json_output=curl_exec($ch); $mydata = json_decode($json_output); $result = $mydata->title; e
[
{
"title": "red",
},
{
"title": "blue",
},
{
"title": "yellow",
},
]
我只想得到第一个数据,瑞德。
我试着用这个
...
$json_output=curl_exec($ch);
$mydata = json_decode($json_output);
$result = $mydata->title;
echo $result[1];
及
但是不要工作
如何从这个json中只获取第一个标题数据?首先,您的json无效。您可以使用验证器检查JSON是否有效。它应该如下所示:
$json_output=curl_exec($ch);
$mydata = json_decode($json_output);
$result = $mydata[0]->title;
echo $result;
[
{
"title": "red"
},
{
"title": "blue"
},
{
"title": "yellow"
}
]
访问JSON对象有两种方式:
对象数组:
$mydata = json_decode($json_output);
$title = $mydata[0]->title; // red
关联数组:
$mydata = json_decode($json_output, true);
$title = $mydata[0]['title']; // red
有关更多信息,请参阅。根据PHP手册,返回以适当PHP类型的JSON编码的值。值true、false和null分别作为true、false和null返回。如果无法解码JSON或编码数据深度超过递归限制,则返回NULL
<?php
$json_output = '[{ "title": "red" }, { "title": "blue" }, { "title": "yellow" }]';
$mydata = json_decode($json_output);
var_dump($mydata);
/* Output:
array(3) {
[0]=>
object(stdClass)#1 (1) {
["title"]=>
string(3) "red"
}
[1]=>
object(stdClass)#2 (1) {
["title"]=>
string(4) "blue"
}
[2]=>
object(stdClass)#3 (1) {
["title"]=>
string(6) "yellow"
}
}
*/
echo $mydata[0]->title;
// Output: red
?>
当第二个参数为TRUE时,返回的对象将转换为关联数组
<?php
$json_output = '[{ "title": "red" }, { "title": "blue" }, { "title": "yellow" }]';
$mydata = json_decode($json_output, TRUE);
var_dump($mydata);
/* Ouput:
array(3) {
[0]=>
array(1) {
["title"]=>
string(3) "red"
}
[1]=>
array(1) {
["title"]=>
string(4) "blue"
}
[2]=>
array(1) {
["title"]=>
string(6) "yellow"
}
}
*/
echo $mydata[0]['title'];
// Output: red
?>
另一方面,访问包含PHP命名约定不允许的任何字符的对象中的元素可以通过用花括号括住索引来完成
<?php
$json_output = '[{ "h1-title": "red" }, { "h1-title": "blue" }, { "h1-title": "yellow" }]';
$mydata = json_decode($json_output);
var_dump($mydata);
/* Output:
array(3) {
[0]=>
object(stdClass)#1 (1) {
["h1-title"]=>
string(3) "red"
}
[1]=>
object(stdClass)#2 (1) {
["h1-title"]=>
string(4) "blue"
}
[2]=>
object(stdClass)#3 (1) {
["h1-title"]=>
string(6) "yellow"
}
}
*/
echo $mydata[0]->{'h1-title'};
// Output: red
?>
var_dump$mydata您看到了什么?json_解码$json_输出,true->$mydata[1]['title']//blue@Xorifelse它是索引0。OP错误地使用索引1来获取第一个条目,这显然是错误的,可能与所有尾随的逗号重复,这不是有效的JSON。对本文的任何解释?$mydata是一个对象数组。因此,您需要访问其索引$mydata[0]。还要添加一个事实,即当前JSON字符串的格式无效。当它失败时返回null。根据您显示的输出[{title:red,},{title:blue,},{title:yellow,},]@GianTomakin我认为此注释不属于此答案。您不必将其转换为数组$title=$mydata[0]->title;同样有效。还要添加一个事实:当前JSON字符串的格式无效。失败时返回null。@否则我添加了它。我还使用了一个JSON验证器,你是对的,我个人不会花太多时间来回答即将结束的问题,尤其是当它是一个级别较低的用户时。他们似乎忘记了他们问了这个问题,甚至不要标记或+1它,但嘿,所有+1来自我的努力。
<?php
$json_output = '[{ "h1-title": "red" }, { "h1-title": "blue" }, { "h1-title": "yellow" }]';
$mydata = json_decode($json_output);
var_dump($mydata);
/* Output:
array(3) {
[0]=>
object(stdClass)#1 (1) {
["h1-title"]=>
string(3) "red"
}
[1]=>
object(stdClass)#2 (1) {
["h1-title"]=>
string(4) "blue"
}
[2]=>
object(stdClass)#3 (1) {
["h1-title"]=>
string(6) "yellow"
}
}
*/
echo $mydata[0]->{'h1-title'};
// Output: red
?>