dongliuxia9495 2014-03-08 11:49
浏览 51
已采纳

如何编辑我的代码,以便它将打印JSON字符串的所有块,而不仅仅是第一个?

I am trying to get the contents of some source files from the same page and save them into MySQL. Some files have a different number of property names than the others so a more "auto" code will help. I am pasting below a sample source file, but you can see it live with my codes below.

Sample source file:

{
Trims: [
{
   "model_id":"15155"
  ,"model_make_id":"ford"
  ,"model_name":"Taurus"
  ,"model_trim":""
  ,"model_year":"2000"
  ,...(all available model fields are included)
 },
{
   "model_id":"15073"
  ,"model_make_id":"ford"
  ,"model_name":"Taurus"
  ,"model_trim":"3.0"
  ,"model_year":"2000"
  ,...(all available model fields are included)
  },
  {etc...}
}]}

Expected output:

I want to print the properties names just once because there are the same in number for all the source file.

model_id
model_make_id
model_name
model_trim
model_year
...and so on....

------
and afterwards all the values

15155
ford
Taurus

2000
...and so on...

15073
ford
Taurus
3.0
2000
...and so on...

FIRST workaround attempt: http://codepad.viper-7.com/dZDxoo

I can print the property names and the values. But with a problem. I need only one time the property names and this is ok. But it prints only the first block of VALUES and not the other VALUES. If I can print everything, this is my solution. The number of property names from different source files I am working, are different so this helps me to get the names automatically.

<?php
$json = file_get_contents('http://www.carqueryapi.com/api/0.3/?cmd=getTrims&year=2007&make=mini');
$vres = array('{"Trims":' , '}]}');
$allakse = array("" , "}]");
$json = str_replace($vres, $allakse, $json);
$cars = json_decode($json, true);

foreach ($cars[0] as $key => $car)
{
  echo "$key", "
";
}

echo"<br><br>";

foreach ($cars[0] as $key => $car)
{
  echo "$car", "
";
}

SECOND workaround attempt: http://codepad.viper-7.com/7bcDUx

Because the source files have a different number of properties, having a fixed for each - echo loop will not help me. This is essential because I want to get the property names automatically from each source file. Also, I can not print the properties names , just the values.

<?php
$json = file_get_contents('http://www.carqueryapi.com/api/0.3/?cmd=getTrims&year=2007&make=mini');
$cars = json_decode($json, true);
foreach($cars['Trims'] as $car){
    echo $car['model_body'].'<br/>';
}

What should I do to echo all the values from all the blocks and not from the first only?

  • 写回答

1条回答 默认 最新

  • dongxun2903 2014-03-08 11:58
    关注

    When you have your decoded json, you can just operate on the Array and print headers from the any item (lets choose the first item) and then print values from all items in the array:

    <?php
    function PrintFieldNames($item)
    {
        foreach($item as $key => $value) 
        {
            echo($key . "<br/>");
        }
    }
    
    function PrintFieldValues($item)
    {
        foreach($item as $key => $value) 
        {
            echo($value . "<br/>");
        }
    }
    
    $json = file_get_contents('http://www.carqueryapi.com/api/0.3/?cmd=getTrims&year=2007&make=mini');
    $cars = json_decode($json, true);
    $cars = $cars['Trims'];
    
    PrintFieldNames($cars[0]);
    foreach($cars as $car)
    {
        PrintFieldValues($car);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看