Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJsonObject: Notation error

QJsonObject: Notation error

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fem_dev
    wrote on last edited by
    #1

    Hi,

    I'm using QJsonObject a lot, but now I got an notation error that is appearing only in one single Qt class of my project.

    I'm always using this notation type:

    double x = json["a"]["b"]["c"].toDouble();
    

    So, inside my Qt class I typed:

    double start = config["temperature"]["start"].toDouble();
    

    But I got this error message:

    error: no viable overloaded operator[] for type 'QJsonValueRef'
    

    If I change the JSON notation to this line below, it works good:

    double start = config["temperature"].toObject().value("start").toDouble();
    

    Why this is happening? Why I can use the first notation above?

    My class:

    #include <QJsonObject>
    
    class Step_Density
    {
    private:
        QJsonObject _config;
    
    public:
        Step_Density();
    };
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What does temperature contain ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What does temperature contain ?

        F Offline
        F Offline
        fem_dev
        wrote on last edited by
        #3

        @SGaist , the config is a QJsonObject.

        The content of config is:

        {
            "temperature": 
                {
                    "start": 0.0,
                    "end": 100.0
                }
        }
        

        What is wrong in the first (clean) notation?

        1 Reply Last reply
        0
        • mranger90M Offline
          mranger90M Offline
          mranger90
          wrote on last edited by
          #4

          This is curious, but I think the problem is that operator[] can return either a QJsonValue or a QJsonValueRef.
          This seems to work:

              qDebug() << "Header info is " << static_cast<QJsonValue>(configObject["Header"])["Tag"].toString();
          

          So I think it's picking QJsonValueRef, since the documentation for QJsonValue clearly states that operator[] is
          Equivalent to calling toObject().value(key).

          F 1 Reply Last reply
          0
          • mranger90M mranger90

            This is curious, but I think the problem is that operator[] can return either a QJsonValue or a QJsonValueRef.
            This seems to work:

                qDebug() << "Header info is " << static_cast<QJsonValue>(configObject["Header"])["Tag"].toString();
            

            So I think it's picking QJsonValueRef, since the documentation for QJsonValue clearly states that operator[] is
            Equivalent to calling toObject().value(key).

            F Offline
            F Offline
            fem_dev
            wrote on last edited by fem_dev
            #5

            @mranger90 thank you,

            I type:

            qDebug() << "Header info is " << static_cast<QJsonValue>(_config["Header"])["temperature"].toString();
            

            and I got:

            Header info is  ""
            

            Yes, the operator [] is Equivalent to calling toObject().value(key), and I'm using this operator [] all the time in my project with QJsonObject.
            My doubt is: Why in this Step_Density class the operator [] is returning a QJsonValueRef and not a QJsonValue as always was.

            UPDATE 1:
            Just to help to understand the error I remove the start node and I got no errors. Like:

            double start = config["temperature"].toDouble();
            

            So, I think that the problem is related with the second JSON node...
            What it ca be?

            1 Reply Last reply
            0
            • mranger90M Offline
              mranger90M Offline
              mranger90
              wrote on last edited by
              #6

              This works for me (Qt 5.13.0, ubuntu 19.04)

              
                  QJsonObject configObject = jsonDoc.object();
                  if (configObject.isEmpty())
                  {
                      qDebug() << "Json file is empty or root type is not an object (i.e. {})." << configObject;
                  }
              
                  qDebug() << "Header info is " << static_cast<QJsonValue>(configObject["Header"])["Temperature"].toDouble();
              

              where the json file is:

              {
                  "Header": {
                      "Tag": "Release 1.0",
                      "Temperature": 42.24
                  }
              }
              
              F 1 Reply Last reply
              0
              • mranger90M mranger90

                This works for me (Qt 5.13.0, ubuntu 19.04)

                
                    QJsonObject configObject = jsonDoc.object();
                    if (configObject.isEmpty())
                    {
                        qDebug() << "Json file is empty or root type is not an object (i.e. {})." << configObject;
                    }
                
                    qDebug() << "Header info is " << static_cast<QJsonValue>(configObject["Header"])["Temperature"].toDouble();
                

                where the json file is:

                {
                    "Header": {
                        "Tag": "Release 1.0",
                        "Temperature": 42.24
                    }
                }
                
                F Offline
                F Offline
                fem_dev
                wrote on last edited by
                #7

                @mranger90, I understand your notation, but I got no point about why this is happening now?

                Please, look my screen.
                I uncommment the "wrong line" after the Debug starts only to be more visible in this image.

                error.jpg

                Could you help me to use the first notation?

                PS.: the type of the private member class called _T.start is double

                1 Reply Last reply
                0
                • mranger90M Offline
                  mranger90M Offline
                  mranger90
                  wrote on last edited by mranger90
                  #8

                  The first notation does not work because, for reasons that are beyond my level of C++ expertise, the return of QJsonObject::operator[] is being interpreted as a QJsonValueRef, NOT a QJsonValue. Hence the cast I made.
                  The only other suggestion is to do it in 2 steps:

                  QJsonValue tempVal = _config["Temperature"];
                  _T.start = tempVal["start"].toDouble();
                  _T.end = tempVal["end"].toDouble();
                  
                  F 1 Reply Last reply
                  2
                  • mranger90M mranger90

                    The first notation does not work because, for reasons that are beyond my level of C++ expertise, the return of QJsonObject::operator[] is being interpreted as a QJsonValueRef, NOT a QJsonValue. Hence the cast I made.
                    The only other suggestion is to do it in 2 steps:

                    QJsonValue tempVal = _config["Temperature"];
                    _T.start = tempVal["start"].toDouble();
                    _T.end = tempVal["end"].toDouble();
                    
                    F Offline
                    F Offline
                    fem_dev
                    wrote on last edited by
                    #9

                    @mranger90 said in QJsonObject: Notation error:

                    he only other suggestion is to do it in 2 steps:

                    Yes, I think that you are right...and your code works good. Thank you.

                    Well..I can use it...no problems! I just want to understand why this is hapenning...
                    I will let this post opened, ok?

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      Leonardo
                      wrote on last edited by
                      #10

                      I know this is an old topic, but I got here from Google, so this might be helpful for others in the future. If we take a look at the qjsonobject.h file, we see:

                          QJsonValue operator[] (const QString &key) const;
                          QJsonValue operator[] (QLatin1String key) const { return value(key); }
                          QJsonValueRef operator[] (const QString &key);
                          QJsonValueRef operator[] (QLatin1String key);
                      

                      What determines the return type is the const keyword. This means that to use the syntax discussed in this thread, we need a const variable.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved