duangou1868 2015-07-10 23:27
浏览 241
已采纳

所以我有一个json文件如何在mysql中插入它?

I'm looking to make a php script to update the mysql every hour from a json file.

the api is that

http://backpack.tf/api/IGetMarketPrices/v1/?key=51f7eb704bd7b8231900000c&appid=730&format=json

How i can copy this things from json and put them in the mysql?

I mean something like that

"AK-47 | Aquamarine Revenge (Battle-Scarred)": {
    "last_updated": 1436569230,
    "quantity": 34,
    "value": 2268
},

"AK-47 | Aquamarine Revenge (Factory New)": {
    "last_updated": 1436569230,
    "quantity": 21,
    "value": 9386
},
"AK-47 | Aquamarine Revenge (Field-Tested)": {
    "last_updated": 1436569230,
    "quantity": 55,
    "value": 4968
},
"AK-47 | Aquamarine Revenge (Minimal Wear)": {
    "last_updated": 1436569230,
    "quantity": 40,
    "value": 6018
},
"AK-47 | Aquamarine Revenge (Well-Worn)": {
    "last_updated": 1436569230,
    "quantity": 40,
    "value": 3597
},
"AK-47 | Black Laminate (Battle-Scarred)": {
    "last_updated": 1436569230,
    "quantity": 50,
    "value": 345
},
"AK-47 | Black Laminate (Factory New)": {
    "last_updated": 1436569230,
    "quantity": 8,
    "value": 8593
},
"AK-47 | Black Laminate (Field-Tested)": {
    "last_updated": 1436569230,
    "quantity": 141,
    "value": 308
},

I want this to go in the mysql like that

enter image description here

This is what I've got so far:

<?php

$json = file_get_contents('http://backpack.tf/api/IGetMarketPrices/v1/?key=51f7eb704bd7b8231900000c&appid=730&format=json');
$obj = json_decode($json,true);

//Database Connection
require_once 'db.php';

/* insert data into DB */
foreach($obj as $item) {
   mysql_query("INSERT INTO `cyberst_CSGO`.`items` ( cost, lastupdate)
   VALUES ('".$item['value']."'', '".$item['last_updated']."')");

}
//database connection close
mysql_close($con);

//}
?>
  • 写回答

1条回答 默认 最新

  • donglian2106 2015-07-11 01:18
    关注

    EDIT: This code generates a temporary tab-delimited file and uses LOAD DATA IN FILE to insert that file into the database then deletes the temporary file.

    1. Use MySQLi - mysql functions are deprecated in PHP now
    2. EDIT: cost = value * 0.01.
    3. Fastest way to load a bunch of data into MySQL is with LOAD DATA INFILE
    4. I haven't tested the database import but I've used this method many times and it works quite well.
    5. You may need to escape $name $cost $row with mysqli_real_escape_string() or $conn->real_escape_string() first. I didn't check all your data.

    Here's the PHP:

    $json = file_get_contents('http://backpack.tf/api/IGetMarketPrices/v1/?key=51f7eb704bd7b8231900000c&appid=730&format=json');
    $obj = json_decode($json,true);
    
    //open a temporary file to hold data
    $path  = str_replace('\\','/',realpath(dirname(__FILE__)));
    $filename = time().'tmp.csv';
    $filename = $path.'/'.$filename;
    $tmpfile = fopen($filename,'w+');
    
    //data to loop through and write to temporary file
    $loopme = $obj['response']['items'];
    
    foreach($loopme as $name=>$row) {
        //i'm guessing this is what cost is
        $cost = $row['value'] * 0.01;
        //write to our tmp file
        fwrite($tmpfile,"$name\t$cost\t$row[last_updated]
    ");
    }
    
    // this is the fastest way to load large amounts of data into MySQL
    $sql = "LOAD DATA INFILE '$filename'
      INTO TABLE `cyberst_CSGO`.`items`
      FIELDS TERMINATED BY '\t' LINES TERMINATED BY '
    '";
    
    //run your mysql query
    
    //delete temporary file
    unlink($filename);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?