Skip to content
Advertisement

PHP convert XML to JSON

I am trying to convert xml to json in php. If I do a simple convert using simple xml and json_encode none of the attributes in the xml show.

JavaScript

So I am trying to manually parse it like this.

JavaScript

and the output for state is {"state":{"0":"Alabama"}} rather than {"state":"Alabama"}

What am I doing wrong?

XML:

JavaScript

Output:

JavaScript

var dump:

JavaScript

Advertisement

Answer

I figured it out. json_encode handles objects differently than strings. I cast the object to a string and it works now.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement