Qt4解析json格式文档
#include <QtGui/QApplication>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptValue>
#include <QtScript/QScriptValueIterator>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString s = "{\"foo\":\"bar\",\"number\":51.3,\"name\":\"hello world\"}";
QScriptEngine engine;
QScriptValue sc = engine.evaluate("value = " + QString(s));
QScriptValueIterator it(sc);
while (it.hasNext())
{
it.next();
qDebug() << it.name() << ": " << it.value().toString();
}
return a.exec();
}
boost解析json
例:
using namespace std;
using namespace boost::property_tree;
// example1
string s = "{\"a\":\"aa\",\"b\":{\"k\":\"kk\"}}";
stringstream stream(s);
read_json<ptree>( stream, pt);
cout<<pt.get<string>("a")<<endl; //输出为"aa"
cout<<pt.get<string>("b.k")<<endl; //输出"kk"
// example2
string s = "{\"data\":[{\"id\":1,\"name\":\"chen\"},{\"id\":2,\"name\":\"zhang\"}]}";
stringstream stream(s);
read_json<ptree>( stream, pt);
ptree pptt,ttt;
pptt = pt.get_child("data");
for (ptree::iterator it = pptt.begin(); it != pptt.end(); ++it){
ttt = it->second; //first为空
cout<<"id="<<ttt.get<int>("id")<<endl;
cout<<"name="<<ttt.get<string>("name")<<endl;
}