Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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将XML转换为Json,并将一些(重复)元素转换为Json数组_Php_Json_Xml_Laravel_Converters - Fatal编程技术网

PHP将XML转换为Json,并将一些(重复)元素转换为Json数组

PHP将XML转换为Json,并将一些(重复)元素转换为Json数组,php,json,xml,laravel,converters,Php,Json,Xml,Laravel,Converters,我的Api(laravel)使用Json代码。现在我还想支持xml。Xml消息必须转换为Json,以便适合现有结构。但我无法将XML与所需的Json格式匹配 示例XML和转换为Json: $xml = "<?xml version='1.0'?> <catalog> <book> <author>Jan</author> <title>Xm

我的Api(laravel)使用Json代码。现在我还想支持xml。Xml消息必须转换为Json,以便适合现有结构。但我无法将XML与所需的Json格式匹配

示例XML和转换为Json:

        $xml = "<?xml version='1.0'?>
    <catalog>
       <book>
            <author>Jan</author>
            <title>Xml to Json Array</title>
            <pages>
                <page>
                    <nr>1</nr>
                    <title>Welcome to my book</title>
                    <pagereviews>
                        <pagereview>
                            <name>maikel</name>
                            <comment>very good</comment>
                        </pagereview>
                        <pagereview>
                            <name>John</name>
                            <comment>i like this page</comment>
                        </pagereview>                            
                    </pagereviews>
                </page>
                <page>
                    <nr>2</nr>
                    <title>more info</title>
                </page>  
                <page>
                    <nr>3</nr>
                    <title>lots of fun</title>
                </page>                              
            </pages>
       </book>
    </catalog>";

    $xmlObject = simplexml_load_string($xml);

    $jsonString = json_encode($xmlObject);

    return $jsonString;
}

我需要没有标签的页面和页面评论。像这样

{
   "book": {
      "author": "Jan",
      "title": "Xml to Json Array",
      "pages": [
         {
            "nr": "1",
            "title": "Welcome to my book",
            "pagereviews": [
               {
                  "name": "maikel",
                  "comment": "very good"
               },
               {
                  "name": "John",
                  "comment": "i like this page"
               }
            ]
         },
         {
            "nr": "2",
            "title": "more info"
         },
         {
            "nr": "3",
            "title": "lots of fun"
         }
      ]
   }
}

我制作了几个与Laravel集成的包来简化XML。您可以在此处查看它们:

  • -将集合转换为XML
  • -转换传入的XML并将其合并到请求对象中
  • -将数据转换为响应的XML
  • -将XML转换为数组

如果您的xml需要在结构上与json不同,我建议您做的是仔细研究。它将为您的数据引入转换器。然后,您可以在使用“我的软件包”之前有条件地设置转换器,以使数据完全按照您的需要输出。

您可以显示您的代码吗?$xmlObject=simplexml\u load\u string($xml)$jsonString=json_encode($xmlObject);返回$jsonString;这只是简单地将一个转换为另一个-保持精确的结构。您需要更改结构,因此需要编写代码来解析数据。从这里开始-然后返回您遇到的具体问题-您需要什么-您想更改原始XML还是已修复?我认为,在转换为json时,您无法更改这一点。但您可以获取数组并删除不需要的levelsim不确定,但看起来请求XML将满足OP要求,不是吗?通常,指向工具或库的链接,或者如果可能,所有上述内容。
{
   "book": {
      "author": "Jan",
      "title": "Xml to Json Array",
      "pages": [
         {
            "nr": "1",
            "title": "Welcome to my book",
            "pagereviews": [
               {
                  "name": "maikel",
                  "comment": "very good"
               },
               {
                  "name": "John",
                  "comment": "i like this page"
               }
            ]
         },
         {
            "nr": "2",
            "title": "more info"
         },
         {
            "nr": "3",
            "title": "lots of fun"
         }
      ]
   }
}