简体   繁体   English

如何在电子renderer.js文件中使用Node.js解析JSON?

[英]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); }); }); 
In the red circle part, the first output is JSON format dataset which server sent. 在红色圆圈部分,第一个输出是服务器发送的JSON格式数据集。 The second output is the result after using JSON.parse(data).status . 第二个输出是使用JSON.parse(data).status之后的结果。 The third output is the result after using JSON.parse(data).info. 第三个输出是使用JSON.parse(data).info之后的结果。 And I think var xxx = JSON.parse(data).info xxx is an array as it's showed in the third output. 而且我认为var xxx = JSON.parse(data).info xxx是一个数组,如第三个输出中所示。 However, what I wanna do is to get the size, time, type, url these value separately in each element in the array. 但是,我想做的是在数组的每个元素中分别获取大小,时间,类型,URL这些值。 But, as you can see, the output of console.log(hhh.info[1].time); 但是,如您所见, console.log(hhh.info [1] .time);的输出 is undefined. 未定义。 Also, I wanna get this array's length, and I just use console.log(hhh.info.length) and the result is 409 and I am confused about it. 另外,我想获取此数组的长度,我只使用console.log(hhh.info.length) ,结果为409,对此我感到困惑。 This result clarify it is a string not an array. 此结果表明它是字符串而不是数组。 And I'd like to get these value and the length of the array at the same time. 我想同时获取这些值和数组的长度。 What should I do? 我该怎么办? Many thanks. 非常感谢。

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.

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