dtol41388
2016-08-07 00:14
浏览 65
已采纳

PHP将嵌套的json保存到mysql数据库

I have some json data that i am retrieving from an external url. It is not importing correctly unless i take out some of the brackets. Anyone know how to properly import this json data? I don't really need the "success", "num_items", "build time" and "updated at". Im a noobie. Thanks!

Here is the php

$filename = "http://www.someurl.com/data.json";
$data = file_get_contents($filename);  
$array = json_decode($data, true);

 foreach($array as $row)  
 {  
      $sql = "INSERT INTO table_all_items(name, quality) VALUES (
      '".$row["name"]."', 
      '".$row["quality"]."'
      )";       
mysqli_query($connect, $sql);
 }  

Here is data.json

{
    "success": true,
    "num_items": 7312,
    "items": [
        {
            "name": "Net",
            "quality": "New"
        },
        {
            "name": "Ball",
            "quality": "New"
        },
        {
            "name": "Hoop",
            "quality": "Used"
        }
    ],
    "build_time": 320,
    "updated_at": 15680
}

图片转代码服务由CSDN问答提供 功能建议

我有一些我从外部网址检索的json数据。 除非我取出一些括号,否则它无法正确导入。 有谁知道如何正确导入这个json数据? 我真的不需要“成功”,“num_items”,“构建时间”和“更新时间”。 我是一个noobie。 谢谢!

这是php

  $ filename =“http://www.someurl.com/data.json”;  
 $ data = file_get_contents($ filename);  
 $ array = json_decode($ data,true); 
 
 foreach($ array as $ row)
 {
 $ sql =“INSERT INTO table_all_items(name,quality)VALUES(
'”。$  row [“name”]。“',
'”。$ row [“quality”]。“'
)”;  
mysqli_query($ connect,$ sql); 
} 
   
 
 

这是data.json

  {  
“成功”:true,
“num_items”:7312,
“items”:[
 {
“name”:“Net”,
“quality”:“New”
},\  n {
“name”:“Ball”,
“quality”:“New”
},
 {
“name”:“Hoop”,
“quality”:“Used”
}  
],
“build_time”:320,
“updated_at”:15680 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtgsl60240 2016-08-07 00:29
    已采纳

    You were looping through the wrong element of your array. As you want to list the items, your loop must look like :

     foreach($array["items"] as $row)  
    
    
    //$filename = "http://www.someurl.com/data.json";
    //$data = file_get_contents($filename);  
    $data='{
        "success": true,
        "num_items": 7312,
        "items": [
            {
                "name": "Net",
                "quality": "New"
            },
            {
                "name": "Ball",
                "quality": "New"
            },
            {
                "name": "Hoop",
                "quality": "Used"
            }
        ],
        "build_time": 320,
        "updated_at": 15680
    }';
    $array = json_decode($data, true);
    $sql = "INSERT INTO table_all_items(name, quality) VALUES ";
     foreach($array["items"] as $row)  
     {  
          $sql = $sql." (
          '".$row["name"]."', 
          '".$row["quality"]."'
          ),";       
     }  
      mysqli_query($connect, substr($sql,0,-1));
    

    I also updated the sql query, for it sends only one request with many values, instead on many requests with one value.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题