如何转换为json对象

时间:2015-10-26 12:31:21

标签: json

我有一个名为' funcJson.json的文件' 其内容如下:

{
        "fid":{
            "processDate":function ()
            {
                data=MainMasterarr;
                for(var i=0;i<data.length;i++)
                {
                    data[i]['_id'] =data[i]['_id'].substring(0,8);  

                    var mongoId = data[i]["_id"];
                    var dateObject = new Date( mongoId );
                dateObject = new Date( parseInt( mongoId, 16 ) * 1000 );
                    var date = dateObject.getDate();
                    var month = dateObject.getMonth()+1;
                    var year = dateObject.getFullYear().toString();
                    var yearSub = year.substring(2,4);
                    if(month<10)
                        month='0'+ month;
                    if (date<10)
                        date='0'+ date;
                    var dateString = month+'/'+date+'/'+yearSub;
                    data[i]['fid'] = dateString; 
                }
            }   ,
            "consoleDate":function ()
            {
                data=MainMasterarr;
                console.log(data[0]['fid']);    
            }
        }
}

现在我尝试读取此文件并将文件内容转换为json对象。 当我将上面的json等同于一个变量时,它就像json对象一样,但是现在当我试图从文件中读取它时会抛出错误。 我的客户端代码如下:

function fetchFileData(fn,callback)
{


    var fileName='funcJson.json';
    var request = new goog.net.XhrIo();
    var data = goog.Uri.QueryData.createFromMap(new goog.structs.Map({
        "fileN":fileName,
    }));

    goog.events.listen(request, "complete", function()
    {
        if (request.isSuccess())
        {
            if(request.getResponseText() == 'fails')
            {
                callback("error");
                return;
            } //if response fails
            else
            {
                var response = request.getResponseJson();
                 fileData=response; 
                 console.log(fileData);
                 callback(response);
           }//else
        } //if request is success
    });//listen event
    request.send(fetchFileUrl, "POST", data); 
    //return fileData;
};

和服务器端代码如下

function fetchFile(req,res,params)
{
    var fs = require('fs');
    //var configJson = {};
    var fileName=params.fileN;
    fs.readFile(fileName, 'utf8', function (err, data) {

      if (err)console.log(err);
      //console.log(data);
      //configJson = JSON.parse(data);
      res.writeHead(200, {
            "Content-Type": "text/plain"
        });//res.writeHead
        res.write(data);
        res.end();
    }); 
}

如果我使用带有函数的普通json给我结果作为对象,我可以实例化为变量并将该变量用作json对象。但是当我在json file.it中给出函数声明时抛出错误。 PLZ指南

1 个答案:

答案 0 :(得分:0)

改变了json格式并且它有效 格式如下:

{
        "fid":{
            "processDate":"function (){data=MainMasterarr;for(var i=0;i<data.length;i++){data[i]['_id'] =data[i]['_id'].substring(0,8);var mongoId = data[i]['_id'];var dateObject = new Date( mongoId );dateObject = new Date( parseInt( mongoId, 16 ) * 1000 );var date = dateObject.getDate();var month = dateObject.getMonth()+1;var year = dateObject.getFullYear().toString();var yearSub = year.substring(2,4);if(month<10)month='0'+ month;if (date<10)date='0'+ date;var dateString = month+'/'+date+'/'+yearSub;data[i]['fid'] = dateString;}}",
            "consoleDate":"function (){data=MainMasterarr;console.log(data[0]['fid']);}"
        },
    "nloc":{
        "processDate":"function (){data=MainMasterarr;for(var i=0;i<data.length;i++){data[i]['_id'] =data[i]['_id'].substring(0,8);var mongoId = data[i]['_id'];var dateObject = new Date( mongoId );dateObject = new Date( parseInt( mongoId, 16 ) * 1000 );var date = dateObject.getDate();var month = dateObject.getMonth()+1;var year = dateObject.getFullYear().toString();var yearSub = year.substring(2,4);if(month<10)month='0'+ month;if (date<10)date='0'+ date;var dateString = month+'/'+date+'/'+yearSub;data[i]['fid'] = dateString;}}",
        "consoleDate":"function (){data=MainMasterarr;console.log(data[0]['fid']);}"
    }
}