从具有功能的JSON文件-到mysql-如何编写良好的代码

时间:2019-05-14 17:43:01

标签: php json

我正在重写功能,以将数据从Json文件获取到mysql。我的代码中有一个错误,我不知道在哪里。如果您希望发挥作用并告诉我在哪里做错了事,我将非常高兴。

json:

[{
"title":"Dumb Ways to Die 3 World Tour","gameType":"html5","description":"Play mini games featuring the characters from Dumb Ways to Die.\n\nDumb Ways to Die is a worldwide brand with over 300 million downloads on Apple App Store and Google Play combined.\n\nTest your reflexes in this challenging series of mini games, where a milisecond can make the difference between winning and losing.\n\nScore coins for each challenge, and use the to repair the once great town of Dumbville.",
"instructions":"Play mini games featuring the characters from Dumb Ways to Die.",
"url":"https://zxxcom/a91f360648334bc988222f1aff557c4e/",
"width":540,
"height":960,
"assetList":[{"name":"https://zxxcom/a91f360648334bc988222f1aff557c4e-512x340.jpeg"},{"name":"https://zxx.com/a91f360648334bc988222f1aff557c4e-512x512.jpeg"},{"name":"https://zxx.com/a91f360648334bc988222f1aff557c4e-512x384.jpeg"},{"name":"https://zxx.com/a91f360648334bc988222f1aff557c4e-1280x720.jpeg"},{"name":"https://zxx.com/a91f360648334bc988222f1aff557c4e-1280x550.jpeg"}],

"categoryList":[{"name":"Arcade"},{"name":"Sports"}],
"tagList":[{"name":"box2d"},{"name":"brain"},{"name":"brand"} ,{"name":"cute"},{"name":"endless"},{"name":"physi cs"},{"name":"simulation"},{"name":"cognitive"}],
"bundleList":[]},

代码:

public function getParameterMap()
{
    return [
        'file' => 'url',
        'category' => ['categoryList']['name'],
        'thumbnail' => ['assetList']['name'],
        'tags' => ['tagList']['name'],
        'name' => 'title'
    ];
}

/**
* @param FeedGame $game
* @param $feedData
* @return mixed
*/
public function filterGame(FeedGame $game, $feedData)
{
    $game->setProviderId(substr(md5($feedData->name), 0, 5));
    $game->setCategory(array([($feedData->categoryList->name)]));
    $game->setThumbnail(array([($feedData->assetList->name)]));
    $name = $feedData->title;
    $description = $feedData->description;
    $game->setTags(array([($feedData->tagList->name)]));
}

在将数据添加到mysql之后-在我的基础上,我在类别列-“数组”中看到。在标签列和缩略图列中,我也看到了“数组”。

在filterGame函数中-这里我出错了,我不知道该怎么写。 Json文件的数组中有数组,我不知道如何 吸引它。

您能帮我吗?

我想在mysql中使用-512x512缩略图和categoryList(json)中的第一类,以及所有标签列表,每个标签后均带有“”。

Ps-对不起我的英语-我是自己学的,可能我犯了很多错误。

var_dump:

array (
  0 => 
  array (
    'title' => 'Dumb Ways to Die 3 World Tour',
    'gameType' => 'html5',
    'description' => 'Play mini games featuring the characters from Dumb Ways to Die.

Dumb Ways to Die is a worldwide brand with over 300 million downloads on Apple App Store and Google Play combined.

Test your reflexes in this challenging series of mini games, where a milisecond can make the difference between winning and losing.

Score coins for each challenge, and use the to repair the once great town of Dumbville.',
    'instructions' => 'Play mini games featuring the characters from Dumb Ways to Die.',
    'url' => 'https://zxx.com/a91f360648334bc988222f1aff557c4e/',
    'width' => 540,
    'height' => 960,
    'assetList' => 
    array (
      0 => 
      array (
        'name' => 'https://zxx.com/a91f360648334bc988222f1aff557c4e-512x340.jpeg',
      ),
      1 => 
      array (
        'name' => 'https://zxx.com/a91f360648334bc988222f1aff557c4e-512x512.jpeg',
      ),
      2 => 
      array (
        'name' => 'https://zxx.com/a91f360648334bc988222f1aff557c4e-512x384.jpeg',
      ),
      3 => 
      array (
        'name' => 'https://zxx.com/a91f360648334bc988222f1aff557c4e-1280x720.jpeg',
      ),
      4 => 
      array (
        'name' => 'https://zxx.com/a91f360648334bc988222f1aff557c4e-1280x550.jpeg',
      ),
    ),
    'categoryList' => 
    array (
      0 => 
      array (
        'name' => 'Arcade',
      ),
      1 => 
      array (
        'name' => 'Sports',
      ),
    ),
    'tagList' => 
    array (
      0 => 
      array (
        'name' => 'box2d',
      ),
      1 => 
      array (
        'name' => 'brain',
      ),
      2 => 
      array (
        'name' => 'brand',
      ),
      3 => 
      array (
        'name' => 'cute',
      ),
      4 => 
      array (
        'name' => 'endless',
      ),
      5 => 
      array (
        'name' => 'physics',
      ),
      6 => 
      array (
        'name' => 'simulation',
      ),
      7 => 
      array (
        'name' => 'cognitive',
      ),
    ),
    'bundleList' => 
    array (
    ),
  ),
  1 => 
  array (
    'title' => 'HeX PuzzleGuys',
    'gameType' => 'html5',
    'description' => 'Connect 4 hexagons of the same color, it\'s that simple!',
    'instructions' => 'Drag and drop pieces and connect at least 4 hexagons of the same color. When you get out of space, the game is over (luckily, you can always restart it!)',
    'url' => 'https://zxx.com/657afaed169b45b6a678637be1912c65/',
    'width' => 800,
    'height' => 600,
    'assetList' => 
    array (
      0 => 
      array (
        'name' => 'https://zxx.com/657afaed169b45b6a678637be1912c65-512x384.jpeg',
      ),
      1 => 
      array (
        'name' => 'https://zxx.com/657afaed169b45b6a678637be1912c65-512x512.jpeg',
      ),
      2 => 
      array (
        'name' => 'https://zxx.com/657afaed169b45b6a678637be1912c65-512x340.jpeg',
      ),
    ),
    'categoryList' => 
    array (
      0 => 
      array (
        'name' => 'Puzzle',
      ),
    ),
    'tagList' => 
    array (
      0 => 
      array (
        'name' => 'hexagon',
      ),
      1 => 
      array (
        'name' => 'connect-4',
      ),
    ),
    'bundleList' => 
    array (
    ),
  ),
)

0 个答案:

没有答案