Skip to content

Instantly share code, notes, and snippets.

@diyism
Last active August 24, 2020 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diyism/ff6bb8ea42914541652a74152e6fac4d to your computer and use it in GitHub Desktop.
Save diyism/ff6bb8ea42914541652a74152e6fac4d to your computer and use it in GitHub Desktop.
smartest json parser
最小的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