【问题标题】:How to traverse array after decoding JSON [duplicate]解码JSON后如何遍历数组[重复]
【发布时间】:2012-10-27 19:20:41
【问题描述】:

可能重复:
PHP JSON decode - stdClass

我有一个 JSON 文件,其结构如下:

[{"key_mappings": "",
  "screen2_thumb": "http://games.mochiads.com/c/g/siegius-arena/screen2_thumb.png",
  "video_url": "http://www.youtube.com/watch?v=S7MDxObgJxw",
  "rating": "Teen",
  "screen1_thumb": "http://games.mochiads.com/c/g/siegius-arena/screen1_thumb.png",
  "metascore": 43.63,
  "height": 500,
  "screen3_thumb": "http://games.mochiads.com/c/g/siegius-arena/screen3_thumb.jpg",
  "stage3d": false,
  "screen3_url": "http://games.mochiads.com/c/g/siegius-arena/screen3.jpg", 
  "recommendation": 5,
  "coins_revshare_enabled": null,
  "category": "Fighting",
  "screen4_thumb": "http://games.mochiads.com/c/g/siegius-arena/screen4_thumb.png",
  "uuid": "6789c7e3-fd27-345e-85d9-e6a58165ae57",
  "author": "Juice-Tin",
  "thumbnail_large_url": "http://games.mochiads.com/c/g/siegius-arena/_thumb_200x200.png",
  "author_link": "https://www.mochimedia.com/community/profile/Juice-Tin",
  "controls":
    [["C", "Item"],
     ["V", "Spell"],
     ["X", "Heavy Attack"],
     ["Z", "Fast Attack"],
     ["fire", "na"],
     ["jump", "na"],
     ["movement", "arrow"]],
  "languages": ["en"],
  "swf_url": "http://games.mochiads.com/c/g/siegius-arena/Siegius%20Arena_.swf",
  "recommended": true,
  "game_tag": "e0e05c5c5fd1a61b",
  "achievements_enabled": false,
  "zip_url": "http://games.mochiads.com/c/g/siegius-arena.zip",
  "screen1_url": "http://games.mochiads.com/c/g/siegius-arena/screen1.png",
  "updated": "2012-10-25T15:47:01.953336-08:00",
  "description": "Fight in arena battles and upgrade your gladiator in this Action-RPG about betrayal and revenge.",
  "tags": ["siegius", "arena", "gladiator", "rome", "battle", "fight", "upgrade", "rpg", "fans", "en"],
  "swf_file_size": 10142842,
  "leaderboard_enabled": false,
  "game_url": "http://www.mochimedia.com/games/siegius-arena",
  "screen2_url": "http://games.mochiads.com/c/g/siegius-arena/screen2.png",
  "slug": "siegius-arena",
  "categories": ["Action", "Fighting"],
  "instructions": "", 
  "name": "Siegius Arena",
  "created": "2012-10-25T12:47:55.080005-08:00",
  "control_scheme": "{\"C\": \"Item\", \"fire\": \"na\", \"jump\": \"na\", \"V\": \"Spell\", \"X\": \"Heavy Attack\", \"Z\": \"Fast Attack\", \"movement\": \"arrow\"}", 
  "popularity": 0,
  "feed_approval_created": "2012-10-25T13:30:53.505710-08:00",
  "coins_enabled": null,
  "thumbnail_url": "http://games.mochiads.com/c/g/siegius-arena/_thumb_100x100.png",
  "screen4_url": "http://games.mochiads.com/c/g/siegius-arena/screen4.png", 
  "alternate_url": "",
  "resolution": "800x500",
  "width": 800}]

然后,我有一个将数据添加到 MYSQL 数据库的 foreach 语句:

foreach($result as $key => $value) {
    if($value) { 
    mysql_query("INSERT INTO `games_db`.`Games` (`title`, `description`, `image`, `category`, `page`, `rating`, `width`, `height`, `tags`) VALUES ('$value->name', '$value->description', '/images/$pageid.jpg', '$category', '$pageid', '$value->rating', '$value->width', '$value->height', '$value->tags')")  
}

我的问题是,我需要做什么才能显示和插入 JSON 文件中的标签?目前,当我运行此页面时,“标签”列的数据只是显示“数组”。

【问题讨论】:

    标签: php mysql json


    【解决方案1】:

    你可以这样使用json_decode()

    $jsonString = '[{"key_mappings": "", "screen2_thumb": "http://games.mochiads.com/c/g/siegius-arena/screen2_thumb.png", "video_url": "http://www.youtube.com/watch?v=S7MDxObgJxw", "rating": "Teen", "screen1_thumb": "http://games.mochiads.com/c/g/siegius-arena/screen1_thumb.png", "metascore": 43.63, "height": 500, "screen3_thumb": "http://games.mochiads.com/c/g/siegius-arena/screen3_thumb.jpg", "stage3d": false, "screen3_url": "http://games.mochiads.com/c/g/siegius-arena/screen3.jpg", "recommendation": 5, "coins_revshare_enabled": null, "category": "Fighting", "screen4_thumb": "http://games.mochiads.com/c/g/siegius-arena/screen4_thumb.png", "uuid": "6789c7e3-fd27-345e-85d9-e6a58165ae57", "author": "Juice-Tin", "thumbnail_large_url": "http://games.mochiads.com/c/g/siegius-arena/_thumb_200x200.png", "author_link": "https://www.mochimedia.com/community/profile/Juice-Tin", "controls": [["C", "Item"], ["V", "Spell"], ["X", "Heavy Attack"], ["Z", "Fast Attack"], ["fire", "na"], ["jump", "na"], ["movement", "arrow"]], "languages": ["en"], "swf_url": "http://games.mochiads.com/c/g/siegius-arena/Siegius%20Arena_.swf", "recommended": true, "game_tag": "e0e05c5c5fd1a61b", "achievements_enabled": false, "zip_url": "http://games.mochiads.com/c/g/siegius-arena.zip", "screen1_url": "http://games.mochiads.com/c/g/siegius-arena/screen1.png", "updated": "2012-10-25T15:47:01.953336-08:00", "description": "Fight in arena battles and upgrade your gladiator in this Action-RPG about betrayal and revenge.", "tags": ["siegius", "arena", "gladiator", "rome", "battle", "fight", "upgrade", "rpg", "fans", "en"], "swf_file_size": 10142842, "leaderboard_enabled": false, "game_url": "http://www.mochimedia.com/games/siegius-arena", "screen2_url": "http://games.mochiads.com/c/g/siegius-arena/screen2.png", "slug": "siegius-arena", "categories": ["Action", "Fighting"], "instructions": "", "name": "Siegius Arena", "created": "2012-10-25T12:47:55.080005-08:00", "control_scheme": "{\"C\": \"Item\", \"fire\": \"na\", \"jump\": \"na\", \"V\": \"Spell\", \"X\": \"Heavy Attack\", \"Z\": \"Fast Attack\", \"movement\": \"arrow\"}", "popularity": 0, "feed_approval_created": "2012-10-25T13:30:53.505710-08:00", "coins_enabled": null, "thumbnail_url": "http://games.mochiads.com/c/g/siegius-arena/_thumb_100x100.png", "screen4_url": "http://games.mochiads.com/c/g/siegius-arena/screen4.png", "alternate_url": "", "resolution": "800x500", "width": 800}]';
    $jsonObject = json_decode($jsonString);
    foreach ($jsonObject->tags as $tag) {
        echo $tag . PHP_EOL;
        //Do something
    }
    

    如果你使用低于 PHP 5.2.0,你可以使用JSON PECL Extension

    我还应该补充一点,您不应再使用 mysql 函数 - 它们已被弃用。见:Why shouldn't I use mysql_* functions in PHP?

    【讨论】:

      【解决方案2】:

      使用json_decode():

      json_decode — 解码 JSON 字符串

      $jsonObject = json_decode($string);   // returns an object
      

      或:

      $jsonArray = json_decode($string, true);  // if second argument is true, returned object will be converted to an associative array
      

      编辑:

      @nickhar 建议:注意:您需要 PHP 5.2.0 或更高版本才能使用 json_decode()

      one of the emulations floating around – @mario

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 2023-01-29
      • 2011-02-05
      相关资源
      最近更新 更多