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": {
            "": {}}}
4
  • have you tried the code before? if yes, post it here. Commented Dec 4, 2018 at 6:14
  • There was other outer loops .I reached here by using forloop.But I dont know how to move forward. @david Commented Dec 4, 2018 at 6:16
  • Where do you get that json from? It would be easier to fix that. Commented Dec 4, 2018 at 6: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 - stackoverflow.com/questions/18906366/…, stackoverflow.com/questions/250679/… Commented Dec 4, 2018 at 7:07

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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']['']);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.