[英]How can I parsing JSON with Node.js in electron renderer.js file?
I'd like to parse JSON formatted dataset from a web server(edit in Electron renderer.js file) 我想从Web服务器解析JSON格式的数据集(在Electron renderer.js文件中进行编辑)
refresh.addEventListener("click", function(){ const http = require('http'); http.get( 'http://teamparamount.cn:8080/Paramount/filesroot?username=test', (resp) =>{ let data = ''; // A chunk of data has been recieved. resp.on('data', (chunk) =>{ data += chunk; }); // The whole response has been received. Print out the result. resp.on('end', () =>{ // console.log(JSON.parse(data).info); // var obj = JSON.stringify(data); var hhh = JSON.parse(data); var xxx = JSON.parse(data).info; // alert(typeof obj); // console.log(hhh.length); // console.log(obj); console.log(data); console.log(hhh.status); console.log(hhh.info); console.log(hhh.info[1].time); console.log(hhh.info.length); console.log(hhh.info[408]); // console.log(obj.info[1]); // console.log(obj.status); // console.log(obj.status.length); function getJsonLth(obj){ var index = 0; for(var i=0;i<obj.length;i++){ if (obj[i] == ':') { index++; } return index; // alert(json1.abc[i].name); } }; console.log(getJsonLth(xxx)); }); }).on("error", (err) => { console.log("Error: " + err.message); }); });
http://teamparamount.cn:8080/Paramount/filesroot?username=test returns this: http://teamparamount.cn:8080/Paramount/filesroot?username=test返回以下内容:
{"status":"success","info":"[{\"size\":\"10105\"...
where info
property is a string, which has to be parsed separately. 其中
info
属性是一个字符串,必须单独进行分析。 That's what you apparently trying to do in: 这显然是您要尝试做的:
var xxx = JSON.parse(data).info;
But instead of JSON.parse(data).info
you should do: JSON.parse(data.info)
. 但是,您应该执行
JSON.parse(data.info)
而不是JSON.parse(data).info
。 Then you will receive your info
array into the xxx
variable. 然后,您将收到
info
数组到xxx
变量中。
it's because the info object is a stringify object, so you need to parse it and override it, and after you will be able to read the entire data object. 这是因为info对象是一个字符串化对象,因此您需要对其进行解析和覆盖,然后才能读取整个数据对象。
var info = JSON.parse(data.info);
data.info = info;
I hope it will help you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.