Reputation: 93
This is the response json I am getting.Please help to parse the json.I used json_decode ,but i don't know how to deal with an object with no name.
{
"child": {
"": {
"rss": [{
"data": "\n \n",
"attribs": {
"": {
"version": "2.0"
}
},
"xml_base": "",
"xml_base_explicit": false,
"xml_lang": "",
"child": {
"": {
"channel": [{
"data": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ",
"attribs": [],
"xml_base": "",
"xml_base_explicit": false,
"xml_lang": "",
"child": {
"": {
"title": [{
"data": "Data name",
"attribs": [],
"xml_base": "",
"xml_base_explicit": false,
"xml_lang": ""
}]
}
}
}]
}
}
}]
}
}
}
I am trying to fetch the value of data inside title.But I dont know to how to solve an object with no name.Can someone please help.
{
"child": {
"": {}}}
Upvotes: 0
Views: 600
Reputation: 147206
There are two ways to access the title
object, dependent on whether you decode the JSON as an object or as an array. If you decode as an object, you need to use the ->{'element'}
notation to get around the empty names (Note this only works in PHP 7.2 and higher):
$json = json_decode($jsonstr);
print_r($json->child->{''}->rss[0]->child->{''}->channel[0]->child->{''}->title);
Output:
Array (
[0] => stdClass Object (
[data] => Data name
[attribs] => Array ( )
[xml_base] =>
[xml_base_explicit] =>
[xml_lang] =>
)
)
As an array you just need to use a blank index (''
):
$json = json_decode($jsonstr, true);
print_r($json['child']['']['rss'][0]['child']['']['channel'][0]['child']['']['title']);
Output:
Array (
[0] => Array (
[data] => Data name
[attribs] => Array ( )
[xml_base] =>
[xml_base_explicit] =>
[xml_lang] =>
)
)
Upvotes: 1
Reputation: 149
May be this helps;
<?php
$json='{
"child": {
"": {
"rss": [{
"data": "\n \n",
"attribs": {
"": {
"version": "2.0"
}
},
"xml_base": "",
"xml_base_explicit": false,
"xml_lang": "",
"child": {
"": {
"channel": [{
"data": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ",
"attribs": [],
"xml_base": "",
"xml_base_explicit": false,
"xml_lang": "",
"child": {
"": {
"title": [{
"data": "Data name",
"attribs": [],
"xml_base": "",
"xml_base_explicit": false,
"xml_lang": ""
}]
}
}
}]
}
}
}]
}
}
}';
$json_decoded=json_decode($json,true);
print_r($json_decoded['child']['']);
?>
Upvotes: 1