简体   繁体   English

如何将从文件读取的JSON数据转换为JSON对象

[英]How to convert a JSON data read from a file as JSON object

I want to pass a JSON object which is read from a file to a POST api. 我想将一个从文件读取的JSON对象传递给POST api。 However, I am seeing that when it is read from the file then it is not working. 但是,我看到从文件中读取它时,它不起作用。 However when the hardcoded JSON data is passed it is working. 但是,当传递经过硬编码的JSON数据时,它就可以工作。 To make you understand better, I am giving the below code example : 为了使您更好地理解,我给出以下代码示例:

var json_content = {};
fs.readfile(file, function(err, data) {
        json_content = data;
        console.log("typeof json_variable  = "+ typeof json_content + "| json_content ="+json_content);
        if(err) {
            console.log("file reading error is = "+err);
            return;
        }
}

The output of the above console.log statement is below : 上面console.log语句的输出如下:

typeof json_variable= object| typeof json_variable = object | json_content = {'title' : 'adventure', 'name': 'Homes'} json_content = {'title':'adventure','name':'Homes'}

When I am sending this above json_content to my post call as a body using superagent, it is not working. 当我使用superagent将json_content以上的内容作为正文发送到我的帖子中时,它不起作用。 In fact in the server it is received as undefined if I do req.body.title 实际上在服务器中,如果我执行req.body.title,它将作为未定义接收

However instead of above mechanism wherein I am reading it from a file and sending, if I set the json_content variable a hardcoded value, then it is working fine. 但是,不是上面的机制(我从文件中读取它并发送),如果我将json_content变量设置为硬编码值,则它可以正常工作。 Below is the code and output : 下面是代码和输出:

json_content = {'title' : 'adventure', 'name': 'Homes'};
console.log("typeof json_variable  = "+ typeof json_content + "| json_content ="+json_content);

The output is different this time as below : 这次的输出不同,如下所示:

typeof json_variable = object |json_content =[object Object] typeof json_variable =对象| json_content = [对象对象]

Please notice the difference in the output as the json_content is now printed as [object Object] unlike earlier case where it was printing the whole JSON. 请注意输出的差异,因为json_content现在被打印为[object Object],这与之前打印整个JSON的情况不同。

Could you please suggest, how do I need to change the first approach when I am reading it from a file and sending it to post to get the similar kind of effect which I am getting while hard coding the json ? 您能否提出建议,当我从文件中读取并将其发送到后期以得到与硬编码json时类似的效果时,我该如何更改第一种方法?

The only difference between them is that one is a string representation of object while second one is javascript object. 它们之间的唯一区别是,一个是对象的字符串表示形式,第二个是javascript对象。

 var json_content = '[{"key":"value"}]'; console.log("json_content"+json_content); var json_content = [{"key":"value"}]; console.log("json_content"+json_content); 

One more thing is happening in your code is that you are using concatenation + in your console.log() statements. 代码中发生的另一件事是,您正在console.log()语句中使用串联+
To me you should avoid using such way and try with , instead: 对我来说,您应该避免使用这种方式,而应尝试使用,而不是:

 var json_content = '[{"key":"value"}]'; console.log("json_content", json_content); var json_content = [{"key":"value"}]; console.log("json_content", json_content); 

For your last comment: 对于您的最后评论:

 var json_content = '[{"key":"value"}]'; json_content = JSON.parse('[{"key":"value"}]'); console.log("json_content"+json_content); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM