Important: Please read the Qt Code of Conduct -

QJsonDocument: JSON.parse() equivalence

  • Hello everyone.

    Because QJsonDocument::fromJSON() supports object and array types only,

    • number values: 1, 2.3
    • boolean values: true, false
    • string values: "foo", "bar"
    • null value: null

    strings above are recognized as invalid JSON documents.

    Also, instantiating QJSEngine and call JSON.parse() with string is not considered because it's too much overhead.

    So, I wonder does Qt has a simple solution that convert a string to JS value like JSON.parse() do?

    Any ideas are welcome :)

  • Something like:

    • number values: QString::toInt()
    • boolean values: QString::compare("true")==0

    Your question is not really clear, where are you getting this values you want to parse from?

  • @VRonin Hi, thank you for the reply.

    The values are from XML files and there're some text values which types are not declared in the document but must be one of the valid JS values.

    I don't really like to use QString based methods to parse them manually, not just I don't know the exact type of a value, but also there're some details behind the JSON.parse(), such as extra spaces, precision problems, etc.

Log in to reply