Last active
August 24, 2020 08:56
-
-
Save diyism/ff6bb8ea42914541652a74152e6fac4d to your computer and use it in GitHub Desktop.
smartest json parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
最小的json parser使用递归逻辑也要100到200行代码: | |
https://github.com/no1msd/json.hpp/blob/master/include/json.hpp | |
https://www.jianshu.com/p/d5eee50c5e24 | |
有没有更巧妙的办法呢? smartest dirty json parser 体现程序员"分形复用"的思维而不要去写if else switch: | |
类似于: | |
<?php | |
$json='{"a":{"b":"c", "k":"jack"},"d":"peter", "x": 1, "t": {"bb":"c", "kk":"jack"}}'; | |
function get($json, $path) | |
{ | |
$json=implode(",\n", explode(',', $json)); | |
$json=preg_replace_callback_array(array('/{[^{}]+}/'=>function($match){return strtr($match[0], array("\n"=>"\n "));}), $json); | |
return $json; | |
} | |
echo get($json, 'a[0].b'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment