dongzhong5756 2014-02-19 09:05
浏览 192
已采纳

如何将以下XML数据转换为数组(JSON或其他)

Whilst there are plenty of options for converting "normal" XML into an array I'd dearly love to find a way of converting this data into an array that I can process with PHP (it's currently designed to be processed by JQuery)

<?xml version="1.0" encoding="ISO-8859-15"?><root><data><![CDATA[ [{title_id: "284270",
          track_id: "1548617",
          artist: [[20670, 1, "Matthias Vogt", "matthias-vogt"]],
          title: "The Wobble Track",
          title_url: "/title/284270/the-wobble-track",
          track_url: "/track/1548617/the-wobble-track",
          label: [88, "Large Music", "large-music"],
          genre: "Deep House",
          genre_url: "/genre/13/deep-house",
          catnumber: "LAR181",
          promo: false,
          duration: "5:54",
          r_date: "2014-02-17",
          price: {hbr: 1.99, wav: 2.74},
          bought: false,
          image: "http://static.traxsource.com/files/images/271306_large.jpg",
          thumb: "http://static.traxsource.com/scripts/image.php/44x44/271306.jpg",
          mp3: "http://preview.traxsource.com/files/previews/88/1324290-p.mp3",
          waveform: "http://static.traxsource.com/files/wf/1324290-wf.png",
          bpm: "120",
          keysig: "Bmin"}
] ]]></data></root>

There are about another 99 objects in this xml string so i've only included 1 for simplicity

I want to convert, what appears to be, an array into an JSON or PHP array - thanks :)

  • 写回答

2条回答 默认 最新

  • douxie7339 2014-02-20 03:52
    关注

    Given that I couldn't solve the issue with any of the solutions I kept digging and did some tests. My solution is not pretty but is certainly functional. I removed all the XML data and left myself with fairly raw JSON. The issue, in this case, with the JSON data is that the strings are not wrapped in "" so I did an str_replace to fix this and it all worked. $content= str_replace('','',str_replace("\'","'",$content)));

    $keys = array("title_id:",
    "track_id:",
    "artist:",
    "title:",
    "title_url:",
    "track_url:",
    "label:",
    "genre:",
    "genre_url:",
    "catnumber:",
    "promo:",
    "duration:",
    "r_date:",
    "price:",
    "hbr:",
    "wav:",
    "bought:",
    "false",
    "image:",
    "thumb:",
    "mp3:",
    "waveform:",
    "bpm:",
    "keysig:"
    );
    $newkeys = array("\"title_id\":",
    "\"track_id\":",
    "\"artist\":",
    "\"title\":",
    "\"title_url\":",
    "\"track_url\":",
    "\"label\":",
    "\"genre\":",
    "\"genre_url\":",
    "\"catnumber\":",
    "\"promo\":",
    "\"duration\":",
    "\"r_date\":",
    "\"price\":",
    "\"hbr\":",
    "\"wav\":",
    "\"bought\":",
    "\"false\"",
    "\"image\":",
    "\"thumb\":",
    "\"mp3\":",
    "\"waveform\":",
    "\"bpm\":",
    "\"keysig\":"
    );
    $content=  str_replace($keys, $newkeys, $content);
    $json = json_decode($content, true);
    

    I was then able to loop through this and process normally.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog