doumei1926 2017-04-11 19:07
浏览 37
已采纳

json_encode创建无用的零键

I am writing an API to output an adress book, which I get the output as a XML. The output is JSON.


What's my problem?

I looping through the entries, which I get from an XML file and store it in an array. That means, that every entry includes an contact:

$xml = new SimpleXMLElement("adressbook.xml"); // <-- Only an example URL
$totalResults = $xml->children('openSearch', true)->totalResults;

$contacts=array();

foreach($xml->entry as $contact){
  $tel = $contact->children('tel', true);
  $entry = array(
    "type"=>$tel->type,
    "name"=>$tel->name,
    "firstname"=>$tel->firstname,
    "street"=>$tel->street,
    "streetno"=>$tel->streetno,
    "zip"=>$tel->zip,
    "city"=>$tel->city,
    "canton"=>$tel->canton,
    "country"=>$tel->country,
    "phone"=>$tel->phone
  );
  array_push($contacts,$entry);
}

After this I want to echo the array in json. I do this with json_encode. But there is the problem. Instead of giving the results directly, it shows me following results:

Output of JSON

The zero's which are red marked should not exist.

What I tried

I researched in the internet and found in the docs and in a few posts on Stack Overflow the JSON_FORCE_OBJECT attribute for json_encode. http://php.net/manual/en/function.json-encode.php

Now my function looks like this:

echo json_encode($contacts,JSON_FORCE_OBJECT);

But I still get the zero's.

Here is also a small example of my XML:

<entry>
    <updated>2017-04-11T02:00:00Z</updated>
    <published>2017-04-11T02:00:00Z</published>
    <title type="text">Mustermann, Max</title>
    <tel:type>Person</tel:type>
    <tel:name>Mustermann</tel:name>
    <tel:firstname>Max</tel:firstname>
    <tel:street>Musterstrasse</tel:street>
    <tel:streetno>17</tel:streetno>
    <tel:zip>8000</tel:zip>
    <tel:city>Zürich</tel:city>
    <tel:canton>ZH</tel:canton>
    <tel:country>ch</tel:country>
    <tel:phone>+123456789</tel:phone>
</entry>

What is going wrong?

UPDATE:

Here is the var_dump:

object(SimpleXMLElement)#6 (15) { ["type"]=> string(12) "Organisation" ["name"]=> string(28) "*******" ["occupation"]=> string(57) "*******" ["street"]=> string(11) "Musterstrasse" ["streetno"]=> string(1) "17" ["zip"]=> string(4) "8000" ["city"]=> string(5) "Zürich" ["canton"]=> string(2) "ZH" ["country"]=> string(2) "ch" ["phone"]=> string(12) "+123456789" }
  • 写回答

1条回答 默认 最新

  • dswm97353 2017-04-11 19:28
    关注

    One approach is to use strval to convert each object to a string

    $xml = new SimpleXMLElement("adressbook.xml"); // <-- Only an example URL
    $totalResults = $xml->children('openSearch', true)->totalResults;
    
    $contacts=array();
    
    foreach($xml->entry as $contact){
      $tel = $contact->children('tel', true);
      $entry = array(
        "type"=>strval($tel->type),
        "name"=>strval($tel->name),
        "firstname"=>strval($tel->firstname,)
        "street"=>strval($tel->street),
        "streetno"=>strval($tel->streetno),
        "zip"=>strval($tel->zip),
        "city"=>strval($tel->city),
        "canton"=>strval($tel->canton),
        "country"=>strval($tel->country),
        "phone"=>strval($tel->phone)
      );
      array_push($contacts,$entry);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题