简体   繁体   English

谷歌字体api / json / php / json_decode

[英]Google font api / json /php /json_decode

I have this json var from google api : 我有来自google api的这个json var:

$json='
{
 "kind": "webfonts#webfontList",
 "items": [ 
{
   "kind": "webfonts#webfont",
   "family": "Jockey One",
   "variants": [
    "400"
   ],
   "subsets": [
    "latin",
    "latin-ext"
   ]
  },
  {
   "kind": "webfonts#webfont",
   "family": "Josefin Sans",
   "variants": [
    "100",
    "100italic",
    "300",
    "300italic",
    "400",
    "400italic",
    "600",
    "600italic",
    "700",
    "700italic"
   ],
   "subsets": [
    "latin"
   ]
  },
....
 ]
}';

How can i decode $json with php in order to display informations like this : 如何解码$ json with php以显示如下信息:

  • Font 1 : family - variant - subsets 字体1:family - variant - subsets
  • Font 2 : family - variant - subsets 字体2:家族-变体-子集

Ex for the second item : 例如第二项:

  • Font family : Josefin Sans | 字体系列:Josefin Sans | variants : 100, 100italic,300...|subsets : latin 变体:100,100italic,300 ... |子集:拉丁语

Thank you 谢谢

$data = json_decode($json,true);
$items = $data['items'];
$i = 0;
foreach ($items as $item) {
    $i++;
    $str = 'Font '.$i.' '.$item['family'].' Subsets:';
    foreach ($item['variants'] as $variant) {
      $str .= ' '.$variant.' ';
    }
    $str.= ' Variants';
    foreach ($item['subsets'] as $subset) {
      $str .= ' '.$subset;
    }
    echo $str.'<br />';
}

This does exactly what you want. 这完全符合你的要求。

I think that the most comfortable way is to create a data structure in your PHP code, and you will decode this JSON to a list of items from this type. 我认为最舒适的方法是在PHP代码中创建数据结构,然后您将将此JSON解码为该类型的项目列表。 Then, it will be very easy to iterate this list and get the data that you want. 然后,迭代此列表并获取所需数据将非常容易。

The data structure that I'm talking about is something that will contain 我正在谈论的数据结构是包含的内容

String : kind
String : family
List<String> : variants
List<String> : subsets

hope this helps... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM