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
smksaAsked:
Who is Participating?
 
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
 
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
 
smksaAuthor Commented:
Thank you ....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.