douzhong1907 2016-11-14 21:58
浏览 73
已采纳

解析JSON使用可变密钥

I try to parse json variables with php codes. Problem is some of array keys contain changeable values for each iteration. By the way this is not a duplicate question, there is nothing like json parsing with changeable variables. Here is my json example and php codes and also output;

{
    "link":"",
    "host":"stream",
    "filename":"Rock Ballads The Best Of 70-90's",
    "icon":"https:\/\/i.ytimg.com\/vi\/JN80ayCfmW0\/hqdefault.jpg",
    "streaming":{
        "audio track in mp3 (54.54MB)":"http:\/\/server.example.parsing.com\/str\/wlqcjv8246\/Rock+Ballads+The+Best+Of+70-90%26%23039%3Bs%28p%29.mp3",
        "360p video in mp4 (80.04MB)":"http:\/\/server.example.parsing.com\/str\/wlqcjyfc33\/Rock+Ballads+The+Best+Of+70-90%26%23039%3Bs%28480p%29.mp4",
        "240p video in mp4 (76.55MB)":"http:\/\/server.example.parsing.com\/str\/wlqck18b72\/Rock+Ballads+The+Best+Of+70-90%26%23039%3Bs%28240p%29.mp4",
        "144p video in mp4 (71.09MB)":"http:\/\/server.example.parsing.com\/str\/wlqck4e410\/Rock+Ballads+The+Best+Of+70-90%26%23039%3Bs%28144p%29.mp4"
    },
    "nb":0,
    "error":"",
    "paws":false
}

and this is the php code that I use;

if($host=="www.youtube.com"){
    $decodeProcess = json_decode($output,true);
    echo ($decodeProcess['filename']);
    echo ($decodeProcess['icon']);
    echo ($decodeProcess['streaming']['audio track in mp3 (54.54MB)']);

}

In this case filename and icon works well but "audio track in mp3 (54.54MB)" or "144p video in mp4 (71.09MB)" is problem for me to parse because everytime the file size changes. What can I do?

  • 写回答

1条回答 默认 最新

  • dpf7891 2016-11-14 22:53
    关注

    Have you considered running a simple mapping operation on the data structure to get it into a more workable form (basically dropping file size information from keys)?

    That might look like:

    if($host=="www.youtube.com"){
        $decodeProcess = json_decode($output,true);
        // array of prefix matches that will be used as as substitution keys
        $key_replacements = [
            'audio' => 'audio',
            '360p video' => 'video_360p',
            '240p video' => 'video_240p',
            '144p video' => 'video_144p'
        ];
        foreach($decodeProcess['streaming'] as $key => $value) {
            foreach ($key_replacements as $prefix => $replacement) {
                if (strpos($key, $prefix) === 0) {
                    $decodeProcess['streaming'][$replacement] = $value;
                    break;
                }
            }
            unset($decodeProces['streaming'][$key]);
        }
    
        echo ($decodeProcess['filename']);
        echo ($decodeProcess['icon']);
        echo ($decodeProcess['streaming']['audio']);
        echo ($decodeProcess['streaming']['video_360p']);
    }
    

    Of course you might want to then encapsulate the overall JSON deserialization and key mapping functionality into its own function or class method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?