[英]why does my JSON file contain [object Object]?
I am using sqlite3 and nodeJS, and querying a database. 我正在使用sqlite3和nodeJS,并查询数据库。 I want to copy the queries into a json file. 我想将查询复制到json文件中。 I run into problems with the json file having strings and objects. 我遇到了具有字符串和对象的json文件的问题。 Why does my JSON file contain: 为什么我的JSON文件包含:
[object Object]
Here is my code: 这是我的代码:
db.all(sql, [], (err, rows) => {
if(err) {
throw err;
}rows.forEach((row) => {
arr_string = JSON.parse(JSON.stringify(row));
fs.writeFile("tempo.json", arr_string, function(err){
});
console.log(arr_string);
});
});
I would like to eventually use ajax requests for these entries. 我想最终将ajax请求用于这些条目。
I think while storing JSON data into SQLite, you're not stringifying it. 我认为将JSON数据存储到SQLite时,不会对它进行字符串化处理。 If you directly store JSON data into SQLite it'll store like [object Object] format. 如果您将JSON数据直接存储到SQLite中,它将以[object Object]格式存储。 My suggestion is to stringify the data while storing in SQLite. 我的建议是在存储在SQLite中的同时对数据进行字符串化处理。 And while retrieving only parse it. 并且在检索时仅解析它。 Then your problem will solve. 这样您的问题就会解决。
arr_string
is actually not a string, as you JSON.parse d it. 正如您使用JSON.parse一样, arr_string
实际上不是字符串。 Remove the JSON.parse
call. 删除JSON.parse
调用。
arr_string = JSON.parse(JSON.stringify(row));
//^---------^--- This is parsing the string back to a new object.
You are parsing the stringified item to a new Object, hence the node.js file writer is trying to write the object to the write stream, resulting in interpreting it as [object Object]
. 您正在将字符串化的项目解析为新的Object,因此node.js文件编写器试图将对象写入写流,从而将其解释为[object Object]
。
Just omit the JSON.parse
, so that the stream will effectively be a string instance: 只需省略JSON.parse
,以使流实际上是一个字符串实例:
arr_string = JSON.stringify(row);
Additionally, you should aggregate the result and write it only one time, or append to the file: 此外,您应该汇总结果并只写入一次,或追加到文件中:
db.all(sql, [], (err, rows) => {
if(err) {
throw err;
}
let _strings = [];
const newline_separator = ''; // <-- use whatever separator you need.
rows.forEach((row) => {
arr_string = JSON.stringify(row);
console.log(arr_string);
_strings.push(arr_string);
});
fs.writeFile("tempo.json", _strings.join(newline_separator), function(err){});
});
Since it's a json, I would suggest you to provide us a more precise input, so that we can guess what the expected result is. 由于它是json,建议您为我们提供更精确的输入,以便我们可以猜测预期的结果。
The JSON.stringify() method converts a JavaScript value to a JSON string JSON.stringify()方法将JavaScript值转换为JSON字符串
arr_string = JSON.stringify(row);
"the problem now with doing that is now the JSON file only writes the last row (and i queried 4) and also I cannot call arr_string.caphi because its not an object anymore" “现在这样做的问题是,现在JSON文件仅写入最后一行(我查询了4),而且我无法调用arr_string.caphi,因为它不再是对象了”
Create an empty array(list) and push each result of query in array.And then
finally convert it into JSON.
sample code : 示例代码:
var rows=[{"holla":"bolla"},{"holla":"bolla1"}];
console.log(JSON.stringify(rows));
JSON.stringify()
takes JSON object and returns String JSON.stringify()
获取JSON对象并返回String
JSON.parse()
takes String and returns JSON object JSON.parse()
接受String并返回JSON对象
try : 尝试:
rows.forEach((row) => {
arr_string = JSON.stringify(row); // 'row' data is converted to String
fs.writeFile("tempo.json", arr_string, function(err){
});`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.