Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用SimpleXML从XML构建JSON的问题_Php_Json_Xml_Simplexml - Fatal编程技术网

Php 使用SimpleXML从XML构建JSON的问题

Php 使用SimpleXML从XML构建JSON的问题,php,json,xml,simplexml,Php,Json,Xml,Simplexml,我正在尝试获取一个书籍列表,我正在从XML解析这些书籍,我希望输出为JSON 我希望JSON的格式为: [ "1" : { "Title": "Sidemen: The Book", "ISBN": "1473648165", "Rating": "4.5" }, ... ] 然而,结果是这样的: [ { "title":{ "0":"Sidemen: The Book" }, "ISBN":{

我正在尝试获取一个书籍列表,我正在从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"
    }
  }
]
我似乎不明白为什么这不是我想要的(问题是因为我是个傻瓜)。这是使用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输出吗?

请使用
SimpleXmlElement
对象,并使用该选项

例如

$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);

您想要的格式不是有效的JSON。。。您能告诉我们当前得到的东西有什么问题吗?另外,您可能希望将这些simplexml元素转换为字符串。例如,
“title”=>(字符串)$item->itemtattributes->title
。另一个问题是,它看起来不像是真正存在的
Rating
。@john stirling,这不是有效的JSON吗?我的输出是否正确?我希望每一本书都链接到IDM下,并进行一些调整。但是,如果标题包含“/”,则添加输出为:“\/\/”。我试过str_replace(“\/”、“/”、$title),但它什么都没做,有什么想法吗?@JamesG,添加JSON\u UNESCAPED\u斜线选项
{
    "1": {
        "title": "Book Title 1",
        "ISBN": "ISBN 1",
        "Rating": "4.5"
    },
    "2": {
        "title": "Book Title 2",
        "ISBN": "ISBN 2",
        "Rating": "5.0"
    }
}