Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a = {"rstatus":0,"stderr":"","stdout":"ctrl_interface=\/var\/run\/wpa_supplicant\nupdate_config=1\nautoscan=exponential:3:60\n\nnetwork={\nssid=\"DCGuest\"\npsk=\"ilikertdc\"\npriority=0\n}\n","truncated":false}
- a = a.stdout
- a = a.split ( '\n' ) .filter ( e => e ) .join ( ',' ) .replace ( /\=\{\,/g, '={' ) .replace ( /\,\}/g, '}' )
- a = parse ( a )
- a = objectify ( a )
- console.log ( a.network.ssid )
- console.log ( a.network.psk )
- function parse ( a ) {
- // a is string
- let depth = 0
- let starti = -1
- let beautyfied = []
- let raw = ''
- for ( let i = 0 ; i < a.length ; ++ i ) {
- if ( a[ i ] === '{' ) {
- if ( depth === 0 ) {
- starti = i
- }
- ++ depth
- }
- else if ( a[ i ] === '}' ) {
- -- depth
- if ( depth === 0 ) {
- beautyfied = [ ... beautyfied, ... beautyfy ( raw ) ]
- beautyfied[ beautyfied.length - 1 ][ 1 ] = parse ( a.slice ( starti + 1, i ) )
- raw = ''
- }
- }
- else {
- if ( depth === 0 ) {
- raw += a[ i ]
- }
- }
- }
- return [ ... beautyfied, ... beautyfy ( raw ) ] .filter ( e => e.length )
- }
- function beautyfy ( a ) {
- // a is string
- return a .split ( ',' ) .map ( e => e.split ( '=' ) .filter ( e => e ) )
- }
- function objectify ( a ) {
- // a is string or array
- if ( typeof a === 'string' ) return a
- const o = {}
- for ( let e of a ) {
- o[ e[ 0 ] ] = objectify ( e[ 1 ] )
- }
- return o
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement