Qt json parser return parse error
I try to parse json string using this method:
QString ourJSONData = QString('{"couchdb":"Welcome","version":"1.0.1"}');
QString response = QString("[") + QString(ourJSONData) + QString("]");
QScrip开发者_开发问答tEngine engine;
QScriptValue sc = engine.evaluate(response);
ui->label->setText(sc.toString());
But label return
SyntaxError: Parse error
I using Qt 4.7.4
What i do wrong? Thanks.
UPD:
Sorry, problem was in that string:
QString ourJSONData = QString('{"couchdb":"Welcome","version":"1.0.1"}');
need change to:
QString ourJSONData = QString("{\"couchdb\":\"Welcome\",\"version\":\"1.0.1\"}");
P.S. this method i found at http://blog.siegerstein.com/archives/134
I built your code in QtCreator in got a very helpful error message:
character constant too long for its type
It's because your ourJSONData
variable is initialised with a text in single quotes, which is for single characters.
This will correct that initialisation. (I put a \
before each double-quote, and then changed the single-quotes to double):
QString ourJSONData = QString("{\"couchdb\":\"Welcome\",\"version\":\"1.0.1\"}");
精彩评论