dpict99695329 2015-09-21 08:13 采纳率: 100%
浏览 39
已采纳

从JSON文件集成并返回值

In my linux box I am running the following php code

<?php
        //Get the response from the server and append timestamp data
        function get_server_response() {
                $curl_post_data = null;
                $service_url = 'http://serverip/source.php';
                $curl = curl_init($service_url);
                curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
                curl_setopt($curl, CURLOPT_USERPWD, "user:pass");
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                $curl_response = curl_exec($curl);
                $response = json_decode($curl_response, true);
                curl_close($curl);
                $time = time();
                $check = $time+date("Z",$time);
                $data['timestamp'] = array('Year' => intval(strftime('%Y', $check)), 'Month' => intval(strftime('%m', $check)), 'Day' => intval(strftime('%d', $check)),
                        'Hour' => intval(strftime('%H', $check)+3), 'Minutes' => intval(strftime('%M', $check)), 'Seconds' => intval(strftime('%S', $check)));

                return array_merge($data,$response);
        }

        if (!file_exists('results.json')) {
                $response = get_server_response();
                $data = new stdClass();
                $data-> Data1 = $response;
                //Create and data to local json file
                $fp = fopen('results.json', 'a+');
                fwrite($fp, json_encode($data, JSON_PRETTY_PRINT));
                fclose($fp);
        } else {
                //Read the data from existing local json file
                $file = file_get_contents("results.json");
                $data = json_decode($file);
                $object = json_decode(json_encode($data), FALSE);
                unset($file);
                //Increment based on the last object and append new key-value pair
                end($object);
                $key = key($object);
                $int = filter_var($key, FILTER_SANITIZE_NUMBER_INT);
                ++$int;
                $response = get_server_response();
                $next = 'Data' . $int;
                $object-> $next = $response;
                //Update the local json file
                file_put_contents('results.json', json_encode($object, JSON_PRETTY_PRINT));
        }
 ?>

Which outputs the following into results.json

{
    "Data1": {
        "timestamp": {
            "Year": 2015,
            "Month": 9,
            "Day": 14,
            "Hour": 16,
            "Minutes": 1,
            "Seconds": 36
        },
        "CG_Uptime": 199481,
        "MHS_Avg": 2661410.65,
        "MHS_5s": 3102456.1,
        "MHS_1m": 2806133.89,
        "MHS_5m": 2700647.04,
        "MHS_15m": 2720732.61,
        "MHS_Now": "2667260",
        "DegC_In": "35",
        "DegC_TopOut": "59",
        "DegC_BotOut": "61",
        "Unit_Uptime": 510403,
        "FreeMem": "403",
        "PSUVolt_Top": "233",
        "PSUVolt_Bot": "235",
        "FAN": "50",
        "VST": "612",
        "VSB": "612",
        "VMAX": "622",
        "AC_TOP": "1100",
        "AC_BOT": "1100",
        "DC_AMP": "150",
        "PSUWall_Top": "640",
        "PSUWatt_Top": "(592",
        "PSUWall_Bot": "640",
        "PSUWatt_Bot": "(596",
        "Total_Watts": 1280
    },
    "Data2": {
        "timestamp": {
            "Year": 2015,
            "Month": 9,
            "Day": 14,
            "Hour": 16,
            "Minutes": 1,
            "Seconds": 59
        },
        "CG_Uptime": 199504,
        "MHS_Avg": 2661347.27,
        "MHS_5s": 2042760.72,
        "MHS_1m": 2595202.5,
        "MHS_5m": 2657730.87,
        "MHS_15m": 2705433.81,
        "MHS_Now": "2667260",
        "DegC_In": "35",
        "DegC_TopOut": "59",
        "DegC_BotOut": "61",
        "Unit_Uptime": 510426,
        "FreeMem": "403",
        "PSUVolt_Top": "233",
        "PSUVolt_Bot": "235",
        "FAN": "50",
        "VST": "612",
        "VSB": "612",
        "VMAX": "622",
        "AC_TOP": "1100",
        "AC_BOT": "1100",
        "DC_AMP": "150",
        "PSUWall_Top": "656",
        "PSUWatt_Top": "(600",
        "PSUWall_Bot": "640",
        "PSUWatt_Bot": "(596",
        "Total_Watts": 1296
    },
    "Data3": {
        "timestamp": {
            "Year": 2015,
            "Month": 9,
            "Day": 14,
            "Hour": 21,
            "Minutes": 25,
            "Seconds": 18
        },
        "CG_Uptime": 218903,
        "MHS_Avg": 2657940.86,
        "MHS_5s": 3701517.96,
        "MHS_1m": 2658126.3,
        "MHS_5m": 2572589.83,
        "MHS_15m": 2584535.75,
        "MHS_Now": "2667260",
        "DegC_In": "34",
        "DegC_TopOut": "57",
        "DegC_BotOut": "60",
        "Unit_Uptime": 529826,
        "FreeMem": "405",
        "PSUVolt_Top": "233",
        "PSUVolt_Bot": "235",
        "FAN": "50",
        "VST": "612",
        "VSB": "612",
        "VMAX": "622",
        "AC_TOP": "1100",
        "AC_BOT": "1100",
        "DC_AMP": "150",
        "PSUWall_Top": "640",
        "PSUWatt_Top": "(596",
        "PSUWall_Bot": "624",
        "PSUWatt_Bot": "(592",
        "Total_Watts": 1264
    }
....
....
}

What I want to do is to add an extra field in the JSON file (named "Total_KWh"), that calculates the total Kilowatt-hours. In other words, to integrate the "Total_Watts" value over time and add the results to each json block. The calculation would be incremental to save cpu. So at "Data1" I start with "Total_KWh":0 then on "Data2" I calculate the time difference in hours (in this case, 23 seconds which is 0.006388 hours), then I multiply with "Total_Watts" value from Data2 and I would get the new "Total_KWh" value. So in this example, "Total_KWh" would be 0.006388*1.296 = 0.00827. Can you help me modify the php code?

  • 写回答

1条回答 默认 最新

  • drqyxkzbs21968684 2015-09-21 09:38
    关注

    It would be easier to add an extra item "timestamp_raw" to every Data* block for calculating time difference between every next pair in Data* set:

    function get_server_response() {
    ...
    curl_close($curl);
    $time = time();
    $check = $time+date("Z",$time);
    $data['timestamp_raw'] = $check;
    ...
    }
    

    Then, we can get through in such way:

    ...
        $key = key($object);
        $int = filter_var($key, FILTER_SANITIZE_NUMBER_INT);
        ++$int;
        $response = get_server_response();
        $next = 'Data' . $int;
        $response["Total_KWh"] = number_format(($response["timestamp_raw"] - $obj->$key->timestamp_raw)/60/60, 6) * ($response["Total_Watts"]/1000);
        $object->$next = $response;
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染