使用SimpleXML从XML构建JSON的问题

时间:2016-11-28 16:58:10

标签: php json xml simplexml

我正在尝试获取一个书籍列表,我正在从XML解析,我希望输出为JSON。

我希望JSON的格式为:

[
  "1" : {
      "Title": "Sidemen: The Book",
      "ISBN": "1473648165",
      "Rating": "4.5"
  },
  ...
]

然而,结果如下:

[
  {
    "title":{
      "0":"Sidemen: The Book"
    },
    "ISBN":{
      "0":"1473648165"
    }
  },
  {
    "title":{
      "0":"DanTDM: Trayaurus and the Enchanted Crystal"
    },
    "ISBN":{
      "0":"1409168395"
    }
  },
  {
    "title":{
      "0":"Pok\u00e9mon Sun & Pok\u00e9mon Moon: The Official Strategy Guide"
    },
    "ISBN":{
      "0":"1911015109"
    }
  },
  {
    "title":{
      "0":"Guinness World Records 2017 Gamer's Edition"
    },
    "ISBN":{
      "0":"1910561398"
    }
  },
  {
    "title":{
      "0":"Minecraft: Blockopedia: An Official Minecraft Book from Mojang"
    },
    "ISBN":{
      "0":"1405273534"
    }
  },
  {
    "title":{
      "0":"Final Fantasy XV - The Complete Official Guide - Collector's Edition"
    },
    "ISBN":{
      "0":"1911015001"
    }
  },
  {
    "title":{
      "0":"Harry Potter: Collectible Quidditch Set"
    },
    "ISBN":{
      "0":"076245945X"
    }
  },
  {
    "title":{
      "0":"Pok\u00e9mon Go The Unofficial Field Guide: Tips, tricks and hacks that will help you catch them all!"
    },
    "ISBN":{
      "0":"1783707712"
    }
  },
  {
    "title":{
      "0":"Minecraft 2017 Annual (by GamesMaster) (2017 Annuals)"
    },
    "ISBN":{
      "0":"0995495025"
    }
  },
  {
    "title":{
      "0":"World of Warcraft The Official Cookbook"
    },
    "ISBN":{
      "0":"1785654349"
    }
  }
]

我似乎无法弄清楚为什么这不是我想要的(probs因为我是菜鸟)。这是由PHP生成的:

$bookList = array();
$id = 0;
foreach ($parsed_xml->Items->Item as $item) {
  $response = file_get_contents($GoodReadsProductLink);
  $parsed_xml = simplexml_load_string($response);

  $currentBook = array(
    "title" => $item->ItemAttributes->Title,
    "ISBN" => $item->ItemAttributes->ISBN,
    "Rating" => $item->ItemAttributes->Rating
  );

  $bookList[$id] = $currentBook;

  $id++;
}

$jsonOutput = json_encode($bookList);


var_dump($jsonOutput);

任何人都可以看到这个问题,并帮我正确格式化JSON输出吗?

1 个答案:

答案 0 :(得分:1)

Cast SimpleXmlElement个对象to string,并使用JSON_FORCE_OBJECT选项。

实施例

$xml = <<<'XML'
<Items>
  <Item Title="Book Title 1" ISBN="ISBN 1" Rating="4.5"/>
  <Item Title="Book Title 2" ISBN="ISBN 2" Rating="5.0"/>
</Items>
XML;
$doc = simplexml_load_string($xml);

$id = 0;
$books = [];
foreach ($doc as $item) {
  if (! $attr = $item->attributes()) {
    continue;
  }

  if (empty($attr['Title']) || empty($attr['ISBN']) || empty($attr['Rating'])) {
    continue;
  }

  $books[++$id] = [
    'title'  => (string)$attr['Title'],
    'ISBN'   => (string)$attr['ISBN'],
    'Rating' => (string)$attr['Rating'],
  ];
}

echo $json = json_encode($books, JSON_FORCE_OBJECT | JSON_PRETTY_PRINT);

输出

{
    "1": {
        "title": "Book Title 1",
        "ISBN": "ISBN 1",
        "Rating": "4.5"
    },
    "2": {
        "title": "Book Title 2",
        "ISBN": "ISBN 2",
        "Rating": "5.0"
    }
}