Convert JSON to 2 string in php

Asked

Viewed 78 times

0

Boas. I have a json file that I wanted to convert into 2 string in php and then write to the database; my json at the moment comes like this:

    [
  [
    1455628560,
    2340
  ],
  [
    1455628620,
    2340
  ],
]

My problem is to select the UNIX Timestamp data and put it in a string and put the values down in another string.

So far I’m working with the following code;

<?php

$sensorid='###';
$token='###';

$interval='###';      #{hour, day, month, year, night}
$unit='###';     #{watt, kwhperyear, eurperyear, audperyear, lpermin, lperday, m3peryear}
$resolution='###';      # {minute, 15min, hour, day, week, month, year, decade, night}


$db_user = "###";
$db_pass = "###";
$db_host = "###";
$db_name = "###";
$table   = "###";




class Flukso {
        private $sensorid, $token, $interval, $unit, $resolution;
        private $url='https://api.flukso.net/sensor/';

        public function __construct($u_sensorid, $u_token, $u_interval, $u_unit, $u_resolution) {
                $this->sensorid = $u_sensorid;
                $this->token = $u_token;
                if (empty($u_interval)) {
                        $this->interval = 'hour';
                } else {
                        $this->interval = $u_interval;
                }      
                if (empty($u_unit)) {
                        $this->unit = 'watt';
                } else {
                        $this->unit = $u_unit;
                }
                $this->resolution = $u_resolution;
                $this->getdata();
        }

        private function getdata() {
        <a href="http://www.flukso.net/content/jsonrest-api-released<br />
        " title="http://www.flukso.net/content/jsonrest-api-released<br />
        ">http://www.flukso.net/content/jsonrest-api-released<br /></a>          
                $header=array();
                $header[]="Accept: application/json";
                $header[]="X-Version: 1.0";
                $header[]='X-Token: '.$this->token;

                $request=$this->url.$this->sensorid.'?interval='.$this->interval.'&unit='.$this->unit.'&resolution='.$this->resolution;

                $ch=curl_init();
                curl_setopt($ch,CURLOPT_URL,$request);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                $this->data=curl_exec($ch);
                curl_close($ch);

        }
        public function __toString() {
                return $this->data;
        }

}



$fluksodata = new Flukso($sensorid, $token, $interval, $unit, $resolution);

$fluksodata=json_decode($fluksodata);



$link = mysql_connect($db_host, $db_user, $db_pass)
       or die ("Error ".date('Y-m-d H:i:s')."Failed to connect to MySQL.\n<b>A fatal MySQL error occured</b>.\nQuery: " . $query . "\nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
mysql_select_db ($db_name)
       or die ("Error ".date('Y-m-d H:i:s')."Failed to connect to the database ".$db_name.".\n<b>A fatal MySQL error occured</b>.\nQuery: " . $query . "\nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));


foreach($fluksodata as $value){


        if ( "$value[1]" != "nan" )
        {

                $corrected_ts = $value[0] - 3660;
                $formatdatetime = date('Y-m-d H:i:s', $corrected_ts);
                $year = date('Y', $corrected_ts);

                $corrected_value = $value[1];

                $query = "insert ignore into $table (ts, value) values ('$formatdatetime', $corrected_value)";
                $result = mysql_query ($query)
                        or die ("Error ".date('Y-m-d H:i:s')."Failed to insert ".$table." record.\nA fatal MySQL error occured.\nQuery: " . $query . "\nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));

        }      
}



mysql_close($link);

?>

Currently the table in mysql looks like this

inserir a descrição da imagem aqui

I changed the code to work and put the data correctly in mysql only missing the code to not copy values if these already existed in the database

1 answer

0

The easiest way to treat json in PHP is with json_decode() and json_encode();

<?php
$jsonStr = "[
  [
    1455628560,
    2340
  ],
  [
    1455628620,
    2340
  ]
]";


$json = json_decode($jsonStr);
echo "<pre>";
var_dump($json);
echo $json[0][0];
echo "\n";
echo $json[1][0];
echo "</pre>";

?>
  • Right, but how do I divide the two values into strings? I can only put everything together at the moment and I wanted to create 2 columns one with "1455628560" and or with "2340" and so on

  • Create 2 columns would insert into the database or display in the browser?

  • put in the two columns of the database I’ll put a print of the table as it gets after putting the data in mysql

Browser other questions tagged

You are not signed in. Login or sign up in order to post.