2

I try to parse json string using this method:

QString ourJSONData = QString('{"couchdb":"Welcome","version":"1.0.1"}');
QString response = QString("[") + QString(ourJSONData) + QString("]");

QScriptEngine 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

1
  • 2
    You should use a real JSON parser to handle JSON instead of a JavaScript engine's eval function. Commented Oct 3, 2011 at 21:33

1 Answer 1

4

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\"}");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.