存档

文章标签 ‘编译’

C++解析json的parser,tinyjson问题的解决办法

2009年4月26日 没有评论

前不久需要解析一个json的接口,上json官网(http://www.json.org/json-zh.html)上找找C++parser。结果发现长长的list,支持各种语言的各种版本。上第一个官网看看(http://blog.beef.de/projects/tinyjson/),对tiny**的东西还是比较感兴趣的,比如tinyxml。。。

果然够tiny,下载下来一看发现只有一个.hpp文件就行了。接下来就开始ft了,按照官网上的sample调用了一下,编译都不过:

../include/tinyjson/tinyjson.hpp: In function ‘typename json::grammar<typename Iterator::value_type>::variant json::parse(const Iterator&, const Iterator&)’:

../include/tinyjson/tinyjson.hpp:549: error: expected `;’ before ‘st’

../include/tinyjson/tinyjson.hpp:550: error: ‘st’ was not declared in this scope

../include/tinyjson/tinyjson.hpp: In function ‘typename json::grammar<typename Iterator::value_type>::variant json::parse(const Iterator&, const Iterator&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]‘:

test.cpp:45: instantiated from here

../include/tinyjson/tinyjson.hpp:549: error: dependent-name ‘json::grammar<typename Iterator::value_type>::stack’ is parsed as a non-type, but instantiation yields a type

../include/tinyjson/tinyjson.hpp:549: note: say ‘typename json::grammar<typename Iterator::value_type>::stack’ if a type is meant

……

google了一下,相关资料真少,难道用c++解析json很发指么,最后硬着头皮在这个日文的bloghttp://d.hatena.ne.jp/S_Nakayama/20081207/1228592806)上找到了答案。修改记录如下:

549c549

< json::grammar< typename Iterator::value_type >::stack st;

> typename json::grammar< typename Iterator::value_type >::stack st;

565c565

< return json::grammar< typename Iterator::value_type >::variant(new boost::any());

> return typename json::grammar< typename Iterator::value_type >::variant(new boost::any());

相应的加上typename就行了。

最后,还想说一句,tinyjson官网的sample里遍历的方法也没编译过,最后改了一下遍历函数的参数,函数原型是void Traverse(json::grammar<char>::variant const v, const string& name, int level)。name表示上级节点名称,level表示当前内容的深度。虽然看着还是不爽,但好歹能工作了。