json_decode not working ?

Hi,

I have the following scripts :

=======================
$jsonData = stripslashes(html_entity_decode($xml));

print_r($jsonData);

$k=json_decode($jsonData,true);

print_r($k);

======================================

When print_r($jsonData); it produce output as follows :

<whmcsapi version="7.4.1">
<action>getinvoice</action>
<result>success</result>
<invoiceid>242347</invoiceid>
<invoicenum></invoicenum>
<userid>25209</userid>
<date>2017-12-21</date>
<duedate>2018-01-11</duedate>
<datepaid>0000-00-00 00:00:00</datepaid>
<lastcaptureattempt>0000-00-00 00:00:00</lastcaptureattempt>
<subtotal>50.00</subtotal>
<credit>0.00</credit>
<tax>3.00</tax>
<tax2>0.00</tax2>
<total>53.00</total>
<balance>53.00</balance>
<taxrate>6.00</taxrate>
<taxrate2>0.00</taxrate2>
<status>Unpaid</status>
<paymentmethod>banktransfer</paymentmethod>
<notes></notes>
<ccgateway></ccgateway>
<items>
<item>
<id>304494</id>
<type>Domain</type>
<relid>52025</relid>
<description>Domain Renewal - xxxxxx.com - 1 Year/s (11/01/2018 - 10/01/2019)</description>
<amount>50.00</amount>
<taxed>1</taxed>
</item>
</items>
<transactions></transactions>
</whmcsapi>

But when print_r($k); , no data displayed.

Anybody can help me on this ?

What is wrong with my json_decode ?

Thank you.
LVL 2
Shamsul KamalJunior TechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
The $jsonData you show above is XML and not JSON.  There is no JSON for json_decode to work with.
0
F IgorDeveloperCommented:
The XML data (xml string) could be converted to a json (string)  structure, and then to an array structure using this code:
(in this case your $jsonData will be $xml_string)

//xml object
$xml = simplexml_load_string($xml_string);
//object to json
$json = json_encode($xml);
//json to array (2nd argument of json_decode() is TRUE, otherwise it will return an object)
$array = json_decode($json,TRUE);

Open in new window



Note: Only tags and text content will be processed, xml attributes will be treated as an array element (@attributes) .


Array
(
    [@attributes] => Array
        (
            [version] => 7.4.1
        )

    [action] => getinvoice
    [result] => success
    [invoiceid] => 242347
    [invoicenum] => Array
        (
        )

    [userid] => 25209
    [date] => 2017-12-21
    [duedate] => 2018-01-11
    [datepaid] => 0000-00-00 00:00:00
    [lastcaptureattempt] => 0000-00-00 00:00:00
    [subtotal] => 50.00
    [credit] => 0.00
    [tax] => 3.00
    [tax2] => 0.00
    [total] => 53.00
    [balance] => 53.00
    [taxrate] => 6.00
    [taxrate2] => 0.00
    [status] => Unpaid
    [paymentmethod] => banktransfer
    [notes] => Array
        (
        )

    [ccgateway] => Array
        (
        )

    [items] => Array
        (
            [item] => Array
                (
                    [id] => 304494
                    [type] => Domain
                    [relid] => 52025
                    [description] => Domain Renewal - xxxxxx.com - 1 Year/s (11/01/2018 - 10/01/2019)
                    [amount] => 50.00
                    [taxed] => 1
                )

        )

    [transactions] => Array
        (
        )

)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shamsul KamalJunior TechAuthor Commented:
Thank you ....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.