Home json Singing the QJSONARRAY array

Singing the QJSONARRAY array

Author

Date

Category

I can not understand how you can pull out the values ​​from this array? I tried in different ways, nothing comes out.

Array of the 111111111111111111111,22222222222222222222,333333333333333333333333333333]

qjsonarray data = jb.value ("test"). Toarray ();
qdebug () & lt; & lt; DATA; // in array 3 elements
// output: QJsonValue (array, QJsonArray ([11111111111111,22222222222222,33333333333333]))
// I trial so, the output is empty.
For (Const QJsonValue & Amp; V: DATA)
{
  qdebug () & lt; & lt; v.tostring ();
}
// ""
// ""
// ""

Answer 1

qstring jsonraw = "[1111, 2222, 3333]";
QvariantList JSonList = QJSondocument :: Fromjson (jsonraw.toutf8 ()). Tovariant (). Tolist ();
For (AUTO & AMP; IT: JSonList)
{
  qdebug () & lt; & lt; IT;
}

Conclusion:

qvariant (double, 1111)
Qvariant (Double, 2222)
QVariant (Double, 3333)

Answer 2

Fully “up to the heap” brute force elements and sub-elements. Hope available.

json

[
 {"Id": 0,
 "Text": "Element1",
 "Subitems": ["SUBELEMENT1.1", "SUBELEMENT1.2"]
 },
 {"Id": 1,
 "Text": "Element2",
 "Subitems": ["SUBELEMENT2.1", "SUBELEMENT2.2"]
 }
]

Code:

qbytearray myjson = "[{\" id \ ": 0, \" text \ ": \" element1 \ ", \ "Subitems \": [\ "SUBELEMENT1.1 \"]}, {\ "id \": 1, \ "text \": \ "element2 \", \ "subitems \ ": [\" SUBELEMENT2.1 \ ", \" Sublement2.2 \ "]}]";
QJSONPARSEERROR ERR;
Qjsondocument document = qjsondocument :: fromjson (myjson, & amp; err);
if (err.error! = QJSONPARSEERROR :: NOERROR) {
  qdebug () & lt; & lt; "Error:" & lt; & lt; err.errorstring ();
  Return 0;
}
Qjsonarray array = document.array ();
Foreach (QJSONVALUE VAL, Array) {
  QJSONObject OBJ = VAL.ToObject ();
  qdebug () & lt; & lt; OBJ.VALUE ("ID"). TINT () & lt; & lt; ":" & lt; & lt; obj.value ("text"). Tostring ();
  QJsonarray Array = Obj.Value ("Subitems"). Toarray ();
  Foreach (QJSONVALUE ARRVAL, ARRAY) {
    qdebug () & lt; & lt; "" & lt; & lt; arrval.tostring ();
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions