node.js - Parse PHP Arrays in JavaScript - Stack Overflow

I've some PHP source code that are simple key-value arrays like these:return array('var1'

I've some PHP source code that are simple key-value arrays like these:

return array('var1' => 'var2' );

And

return array('sub' => array( 'var1' => 'var2' ) );

And I need to parse them into JavaScript objects, because I've a JavaScript implementation of a PHP library and I want to test the patibility using the original test cases.

There are over a 100 tests, so manual conversion is not practical.

Is there an easy way to convert these into JavaScript objects without using PHP?

I've some PHP source code that are simple key-value arrays like these:

return array('var1' => 'var2' );

And

return array('sub' => array( 'var1' => 'var2' ) );

And I need to parse them into JavaScript objects, because I've a JavaScript implementation of a PHP library and I want to test the patibility using the original test cases.

There are over a 100 tests, so manual conversion is not practical.

Is there an easy way to convert these into JavaScript objects without using PHP?

Share Improve this question edited Apr 20, 2013 at 9:41 GameAlchemist 19.3k7 gold badges37 silver badges59 bronze badges asked Apr 20, 2013 at 8:17 mabsmabs 9971 gold badge10 silver badges17 bronze badges 2
  • 1 Why not use JSON? Why without PHP? – elclanrs Commented Apr 20, 2013 at 8:28
  • Json is fine but since the test framework runs in a node.js app PHP isn't a good option. – mabs Commented Apr 20, 2013 at 11:24
Add a ment  | 

4 Answers 4

Reset to default 4

To actually answer your question – how to parse PHP's associative arrays to JSON without using PHP – let's use some JavaScript code.

"array('sub' => array( 'var1' => 'var2' ) );".replace(/array\(/g, '{').replace(/\)/g, '}').replace(/=>/g, ':').replace(/;/g, '').replace(/'/g, '"');

This is assuming you just happen to sit on some source code and want to copy it into a Node.js application, and that all the data looks exactly like this. If the data happens to be on multiple lines, if you even want to parse away the "return"/";" parts, if some of the data contains indexed arrays, or if any of the values contain the string I just naively parse away, you'll have to make this script a bit smarter.

And as others have said, if you're interacting with a PHP service, just use json_encode().

This should work!

<?# somefile.php ?>
<script type="text/javascript">
    var json = '<?= json_encode(array('sub' => array( 'var1' => 'var2' ))) ?>';
    var object = JSON.parse(json);
    console.log(object);
</script>

Output

{
  sub: {
    var1: "var2"
  }
}

More monly, you will see a language-agnostic API which simply provides JSON responses. You could easily test this using asynchronous requests to the API from JavaScript (in-browser, via Node.js, etc.);

If I understood you correctly, you need to use json_encode() like this:

return json_encode(array('sub' => array( 'var1' => 'var2' )));

The returned value: {"sub":{"var1":"var2"}}

You can use json_encode() in PHP and JSON.parse() in JavaScript.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744726102a4590187.html

相关推荐

  • node.js - Parse PHP Arrays in JavaScript - Stack Overflow

    I've some PHP source code that are simple key-value arrays like these:return array('var1'

    23小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信