我想从 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返回:
{"status":"success","info":"[{\"size\":\"10105\"...
其中info
property 是一个字符串,必须单独解析。这就是你显然想要做的:
var xxx = JSON.parse(data).info;
但不是JSON.parse(data).info
你应该做的:JSON.parse(data.info)
。然后你会收到你的info
数组到xxx
变量中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句