dqch34769 2014-07-29 22:50
浏览 30
已采纳

将JSON文件从PHP上传到JQuery

I have a large csv dataset I need to transfer from a PHP server to JQuery using JSON. I am working on an embedded device with limited resources - so cannot read the whole file into memory. I have written a function to convert the csv file to a JSON file and then transfer the JSON file to the client, however I get a JSON syntax error on the client when requesting data using a JQuery AJAX call.

Convert CSV to JSON

public function convert_csv($csv, $json)
{
    /*
     * Open files.
     */
    $csv_handle = fopen($csv, 'r');
    $json_handle = fopen($json, "w");
    /*
     * Get the table headers.
     */
    $headers = fgetcsv($csv_handle);
    /*
     * Write the array name.
     */
    fwrite($json_handle, "\"LogData\":[");

    $FirstRecord = true;
    while ($row = fgetcsv($csv_handle))
    {
        /*
         * Ensure no trailing comma after last record.
         */
        if (!$FirstRecord)
            fwrite($json_handle, ",");
        else
            $FirstRecord = false;
        /*
         * Create JSON record and write to file.
         */
        $complete = array_combine($headers, $row);
        fwrite($json_handle, json_encode($complete));
    }
    /*
     * Close the array
     */
    fwrite($json_handle, "]");
    /*
     * Close the files.
     */
    fclose($csv_handle);
    fclose($json_handle);
}

This is working:

CSV File

TimeStamp,Value
1390364805600.01,2.0
1390451205600.01,3.0
1390537605600.01,0.5
1390546245600.02,23.0
1390563525599.99,0.8

Creates JSON File

"LogData":[{"TimeStamp":"1390364805600.01","Value":"2.0"},{"TimeStamp":"1390451205600.01","Value":"3.0"},{"TimeStamp":"1390537605600.01","Value":"0.5"},{"TimeStamp":"1390546245600.02","Value":"23.0"},{"TimeStamp":"1390563525599.99","Value":"0.8"}]

PHP File Server

The following code is use to serve the JSON data from the server:

$this->logging_model->convert_csv($path, "/tmp/log.json");
//header('Content-Type: application/json');  // I have tried with and without this line
readfile("/tmp/log.json"); // push it out

I can confirm from within a browser that the JSON file as shown above is successfully transferred.

JQuery Client

I use the following the request the data from the client:

$.ajax({
    url: URL, 
    type: "GET",
    dataType: "json",
    success: function(data) { SaveData(PointId, data);},
    error: function (request, status, error) { alert(error);},
    async: false
});

However I get a "SyntaxError: Invalid character" error when I attempt to download the data. I'm not sure where I have gone wrong - but any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • duangu1645 2014-07-29 22:57
    关注

    There are 2 Braces left in Your JSON String. Its not valid at the moment.

    Try

    {"LogData":[{"TimeStamp":"1390364805600.01","Value":"2.0"},{"TimeStamp":"1390451205600.01","Value":"3.0"},{"TimeStamp":"1390537605600.01","Value":"0.5"},{"TimeStamp":"1390546245600.02","Value":"23.0"},{"TimeStamp":"1390563525599.99","Value":"0.8"}]}
    

    And you can use http://jsoneditoronline.com/ to check and proove your JSON Data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题