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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: