0

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": {
            "": {}}}
Vijay J
  • 93
  • 8
  • have you tried the code before? if yes, post it here. – david Dec 04 '18 at 06:14
  • There was other outer loops .I reached here by using forloop.But I dont know how to move forward. @david – Vijay J Dec 04 '18 at 06:16
  • Where do you get that json from? It would be easier to fix that. – M. Eriksson Dec 04 '18 at 06:17
  • 1
    Let me guess - is this some XML to array output. If you stick with using XML there are all sorts of resources for that - https://stackoverflow.com/questions/18906366/parsing-an-rss-feed-in-php-with-dom, https://stackoverflow.com/questions/250679/best-way-to-parse-rss-atom-feeds-with-php – Nigel Ren Dec 04 '18 at 07:07

2 Answers2

1

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']['']);
?>
DoMajor7th
  • 109
  • 5
1

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] =>
    ) 
)

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95