node.js - Parse PHP Arrays in JavaScript -
i've php source code simple key-value arrays these:
return array('var1' => 'var2' );
and
return array('sub' => array( 'var1' => 'var2' ) );
and need parse them javascript objects, because i've javascript implementation of php library , want test compatibility using original test cases.
there on 100 tests, manual conversion not practical.
is there easy way convert these javascript objects without using php?
to answer question – how parse php's associative arrays json without using php – let's use javascript code.
"array('sub' => array( 'var1' => 'var2' ) );".replace(/array\(/g, '{').replace(/\)/g, '}').replace(/=>/g, ':').replace(/;/g, '').replace(/'/g, '"');
this assuming happen sit on source code , want copy node.js application, , data looks this. if data happens on multiple lines, if want parse away "return"/";" parts, if of data contains indexed arrays, or if of values contain string naively parse away, you'll have make script bit smarter.
and others have said, if you're interacting php service, use json_encode()
.
Comments
Post a Comment