3

I have a problem with querying a sub-item from a sub-item.

JSON file:

{
    "user": {
        "url": "www",
        "set": {
            "s_a": 1,
            "s_b": 2,
            "s_c": 3,
            "s_d": 4,
            "s_e": 5
        }
    }
}

Qt:

QString value = jsonfile
                .object()
                .value("user")
                .toObject()
                .value("url")
                .toString();

Qutput:

www

Question: Why doesn't the following work?

QString value = jsonfile
                .object()
                .value("user")
                .toObject()
                .value("set")
                .toObject()
                .value("s_a")
                .toString();
Sonya
  • 53
  • 5

1 Answers1

2

I guess it's because s_a is not a string value but an Int one.

This should work:

int value = jsonfile
            .object()
            .value("user")
            .toObject()
            .value("set")
            .toObject()
            .value("s_a")
            .toInt();
Irda
  • 81
  • 4
  • I have searched so long on subitem functions that I have not seen the obvious! Thank you very much for your answer. – Sonya Oct 05 '18 at 09:04
  • Of course this works "only" for s_a, from s_b you get only zero back as value. – Sonya Oct 05 '18 at 12:29
  • According to the json you showed, it should work with any of `s_*` field simply by replacing the name. What code did you use? – Irda Oct 05 '18 at 13:11
  • I had a memory conflict that caused this error from value 2, and returned 0. The code you posted is 100% correct! – Sonya Oct 05 '18 at 15:51