dougang5088 2015-01-15 09:43
浏览 129
已采纳

PHP> json_encode,即使它只有一个元素,也强制创建一个数组

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


UPDATE

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;
}
  • 写回答

2条回答 默认 最新

  • duanduo3712 2015-01-15 12:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制