douhong9210 2015-06-24 18:53
浏览 39
已采纳

如何从内容中获取价值? [关闭]

{"prereqs":{"prereq":{"type":"prereq_check","value":"submerging_island_feature_enabled"}},"divisions":{"division":[{"items":{"item":[{"name":"rhino_shell","rarity":"common"},{"name":"walrus_wavy","rarity":"special"},{"name":"hippo_fancyshell","rarity":"rare"},{"name":"rhino_jellyfish","rarity":"superRare"}]},"name":"rubyCount_30"},{"items":{"item":[{"name":"walrus_clam","rarity":"common"},{"name":"hippo_nautical","rarity":"special"},{"name":"giraffe_coral","rarity":"rare"},{"name":"elephant_starburst","rarity":"superRare"}]},"name":"rubyCount_40"},{"items":{"item":[{"name":"giraffe_waverider","rarity":"common"},{"name":"pony_sea","rarity":"special"},{"name":"magicdeer_seadeer","rarity":"rare"},{"name":"pony_seaprincesscorn","rarity":"superRare"}]},"name":"rubyCount_50"},{"items":{"item":[{"name":"bigcat_crystallion","rarity":"common"},{"name":"magicdeer_midnightdeer","rarity":"special"},{"name":"horse_ofthesea","rarity":"rare"},{"name":"horse_wingedsea","rarity":"superRare"}]},"name":"rubyCount_60"}]},"crafting":{"recipes":{"recipe":[{"name":"qdke"},{"name":"sb1p"},{"name":"cb8v"}]}},"listEndDate":"07/13/2015","currencyItem":{"name":"healingpotionbottle"},"feed":{"throttleTime":"21600"},"name":"submerging_island"}

  • 写回答

1条回答 默认 最新

  • dtoqa66028 2015-06-24 19:16
    关注

    To get you going my 2 cents. First off, Welcome, please read How to ask a good question

    First you need to decode the json string in to an array. with that array you can get the values.

    <?php
    
    $json = '{"prereqs":{"prereq":{"type":"prereq_check","value":"submerging_island_feature_enabled"}},
    "divisions":{"division":[{"items":{"item":[{"name":"rhino_shell","rarity":"common"},
    {"name":"walrus_wavy","rarity":"special"},{"name":"hippo_fancyshell","rarity":"rare"},
    {"name":"rhino_jellyfish","rarity":"superRare"}]},"name":"rubyCount_30"},
    {"items":{"item":[{"name":"walrus_clam","rarity":"common"},{"name":"hippo_nautical","rarity":"special"},
    {"name":"giraffe_coral","rarity":"rare"},{"name":"elephant_starburst","rarity":"superRare"}]},"name":"rubyCount_40"},
    {"items":{"item":[{"name":"giraffe_waverider","rarity":"common"},{"name":"pony_sea","rarity":"special"},
    {"name":"magicdeer_seadeer","rarity":"rare"},
    {"name":"pony_seaprincesscorn","rarity":"superRare"}]},"name":"rubyCount_50"},
    {"items":{"item":[{"name":"bigcat_crystallion","rarity":"common"},{"name":"magicdeer_midnightdeer","rarity":"special"},
    {"name":"horse_ofthesea","rarity":"rare"},
    {"name":"horse_wingedsea","rarity":"superRare"}]},"name":"rubyCount_60"}]},"crafting":{"recipes":{"recipe":[{"name":"qdke"},
    {"name":"sb1p"},{"name":"cb8v"}]}},"listEndDate":"07/13/2015","currencyItem":{"name":"healingpotionbottle"},"feed":{"throttleTime":"21600"},"name":"submerging_island"}';
    
    //decode the json
    $decoded = json_decode($json, true);
    
    
    // uncomment if you want it to be easier to read
    // echo "<pre>";
    // print_r($decoded);
    // echo "</pre>";
    
    //if you want the singe value, i.e. name of the first item.
    echo "If you want a sinle value:<br>";
    echo $decoded['divisions']['division'][0]['items']['item'][0]['name'] . "<br>";
    
    //to get all names from one item  you need to use a foreach() loop. 
    echo "<br>if you want all names from one item:<br>";
    foreach($decoded['divisions']['division'][0]['items']['item'] AS $value){
        echo $value['name'] . "<br>";
    }
    
    //to get all names we need to use 2 foreach loops because this is nested in multiple arrays
    echo "<br>if you want all names from all items:<br>";
    foreach($decoded['divisions']['division'] AS $value){
        foreach($value['items']['item'] AS $value){
            echo $value['name'] . "<br>";
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 jscolor 赋值input 没能引起前边色框的颜色变化
  • ¥100 驱动程序在\device\raidport1 上检测到控制器错误
  • ¥15 JS报错变量未定义,如何解决?
  • ¥20 找辅导 初学者 想实现一个项目 没有方向
  • ¥15 研究方向遥感场景识别分类。求深度学习基础课程
  • ¥15 关于渗漏场的电场分布模拟
  • ¥24 matlab怎么修改仿真初始时间
  • ¥15 两分段线性回归模型分析阈值效应
  • ¥15 前端和后端代码都没报错,但是点登录没反应的?
  • ¥100 需要远程解决QSQLITE问题!