LinkM LinkM - 5 months ago 32
JSON Question

Convert SSID list to JSON/Array

I'm trying to make it possible to choose different SSID's to switch the Wlan you are connected to from Browser.

var sys = require('sys');
var exec = require('child_process').exec;
app.get(prefix + '/wlan', function(req, res){
child = exec("iwlist wlan0 scan | grep ESSID", function(error, stdout, stderr){
if(error !== null){
console.log('Exec error ' + error);
}
else {
res.send(stdout);
}
});
});


This is my code so far to get a SSID list..

The Output is like that:


ESSID:"WLAN-GUEST" ESSID:"WLAN1" ESSID:"WLAN-GUEST" ESSID:"WLAN1" ESSID:"WLAN2"


I have no idea why two ESSID's are listed twice but my main question is, how can I parse this to JSON or how can I access each entry like an array (wlanlist[0])?

Edit:
I tried to stdout.replace(" ",", "); and JSON.parse but as it's async it's sent without changes. (Not sure if that would work as sync)

Edit2: Trying to access the data like that:

$(document).ready(function() {
$.get(prefix + '/wlan', function(wlanlist){

document.getElementById("wlanoptions").options[0] = new Option("Select your WLAN:","");
document.getElementById("wlanoptions").options[1] = new Option(wlanlist[0],wlanlist[0])
});
});

Answer

This should return an array of SSIDs:

stdout.split("ESSID:")

Now clean up the " and you are all done.