Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QJSEngine evaluate JSON error.



  • I have an instance of the QJSEngine, I also have a JSON variable that I want to add for global access:

    QString strData("{\"db\":\"test\",\"host\":\"localhost\",\"usr\":\"root\",\"pass\":\"password\"}");
    

    My instance of the QJSEngine, pobjScriptEng:

    QJSValue objResult = objScriptEng->evalulate(strData);
    

    The issue is that the above results in an error, where the error is:

    SyntaxError: Expected token `,'
    

    As far as I can see there is nothing wrong with the syntax of the object?



  • @SPlatten
    I'm not an expert, but you seem to be evaluating just

    { "db": "test", ... }
    

    I don't think that's a legal JS statement.

    Don't you intend something more like:

    var something = { "db": "test", ... };
    

    ? (Hmm, perhaps not... anyway, thought for you...)



  • @SPlatten
    I'm not an expert, but you seem to be evaluating just

    { "db": "test", ... }
    

    I don't think that's a legal JS statement.

    Don't you intend something more like:

    var something = { "db": "test", ... };
    

    ? (Hmm, perhaps not... anyway, thought for you...)



  • I'm not sure, the next bit of source when there is no error:

    if ( !objResult.isError() ) {
        objGlobal.setProperty(strName, objResult);
    }
    

    Where strName contains the variable name and objGobal is the globalObject from QJSEngine.

    @JonB, thank you, I modified the code to:

    strData = clsScriptHelper::strStrip(strName + clsXMLnode::msccGlobalDelimiter + strData);
    

    Which translates to:

    db={"db":"test","host":"localhost","usr":"root","pass":"password"}"
    

    Now there is no error and it works, odd that the variable name if required in the definition here when its then specified with the setProperty too.


Log in to reply