I take an xml from an external service via POST , and return the xml in json
this is an example of the xml
<xml>
<item>
<user>utente1</user>
<psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw>
<code>A722C63DB8EC8625AF6CF71CB8C2D939</code>
</item>
<item>
<user>utente2</user>
<psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw>
<code>A722C63DB8EC8625AF6CF71CB8C2D939</code>
</item>
</xml>
and with this procedure i transforms the xml in json
php
$xml = simplexml_load_string($getPostData);
$json = json_encode($xml);
json result
{
"item": [
{
"user": "utente1",
"psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
"code": "25BBDCD06C32D477F7FA1C3E4A91B032"
},
{
"user": "utente2",
"psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
"code": "25BBDCD06C32D477F7FA1C3E4A91B032"
}
]
}
and this is correct. but there is a problem when the xml have only one item
<xml>
<item>
<user>utente1</user>
<psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw>
<code>A722C63DB8EC8625AF6CF71CB8C2D939</code>
</item>
</xml>
in this case the json is
{
"item": {
"user": "utente1",
"psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
"code": "25BBDCD06C32D477F7FA1C3E4A91B032"
}
}
item is an object in this case , and not an array with one element.
I wish it were so
{
"item": [
{
"user": "utente1",
"psw": "A722C63DB8EC8625AF6CF71CB8C2D939",
"code": "25BBDCD06C32D477F7FA1C3E4A91B032"
}
]
}
I read the documentation of json_encode and the various json constants , but i can't find an option to force this procedure. I proceed with a manual control or i can do it automatically? thank you
My solution
//$getPostData = "<xml><item><user>utente1</user><psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw><code>25BBDCD06C32D477F7FA1C3E4A91B032</code></item><item><user>utente1</user><psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw><code>25BBDCD06C32D477F7FA1C3E4A91B032</code></item></xml>";
$getPostData = "<xml><item><user>utente1</user><psw>A722C63DB8EC8625AF6CF71CB8C2D939</psw><code>25BBDCD06C32D477F7FA1C3E4A91B032</code></item></xml>";
$xml = simplexml_load_string($getPostData);
$json = json_encode($xml);
$jsonCheck = json_decode($json);
$numItem = sizeof($jsonCheck->item);
if($numItem == 1){
$newJson = new stdClass();
$newJson->item = array();
$newJson->item[0]["user"] = $jsonCheck->item->user;
$newJson->item[0]["psw"] = $jsonCheck->item->psw;
$newJson->item[0]["code"] = $jsonCheck->item->code;
$newJson = json_encode($newJson);
echo $newJson;
}else{
echo $json;
}
If this is the structure of the entire XML and not just a fragment from something bigger, you can turn it into an array or a mix of arrays and objects using less code:
$xml = simplexml_load_string($getPostData);
$array = array('item' => array());
foreach ($xml->item as $item) {
$array['item'][] = (object)(array)$item;
}
echo(json_encode($array));
This will produce the output you described in the question as expected, no matter how many <item>
elements appear in the XML.
Remove the (object)
from the code inside the foreach()
to get the <item>
elements converted to arrays instead of stdClass
objects.
I had the same issue, took me some time to figure out but I fixed it like this.
$somearray = array();
$_SESSION['activeChatBoxes'] = array_merge($somearray, $_SESSION['activeChatBoxes']);
If the session var activeChatBoxes only contains one item, it would be json_encoded as an Object. After merging into an empty array, the json_encode properly shows an array like
["string"]
Instead of:
{ 1, "string" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With