The QT5 development environment integrates a library of parsing JSON expressions, which is very convenient to use. Friendship hint, as if in the QT4 environment, need to download the relevant library files on the official website to use the resolution function. Words don't say much, on the code
1. Add in Pro file
QT + = Script
2. Add header File
#include <QtScript/QScriptEngine>
3. Add the following code where it needs to be resolved:
For example, the JSON expression that needs parsing is:
The parsing code is:
void mainwindow::replyfinished (Qnetworkreply * reply) {Qbytearray data = Reply->readall (); QString str (data); Qmessagebox::information (This, "Reslut", str); Qscriptengine engine; Qscriptvalue sc = engine.evaluate ("value=" + str); if (Sc.property ("ErrorCode"). ToString ()! = "0") {qmessagebox::information (this, "error", "Query error, please requery!") "); return; }//outer jsom expression, translation result This->ui->outedit->settext (Sc.property ("translation"). toString ()); Second-level JSON expression, network extension qscriptvalue subsc = Sc.property ("web"); Qscriptvalue SUBSC1 = subsc.property (0); Qscriptvalue SUBSC2 = subsc.property (1); Qscriptvalue subsc3 = Subsc.property (2); This->ui->extendedit->settext (Subsc1.property ("key"). ToString () + ":"); This->ui->extendedit->append (Subsc1.property ("value"). toString ()); This->ui->extendedit->append (Subsc2.property ("key"). ToString () + ":"); This->ui->extendedit->append (Subsc2.property ("value"). ToString ()); This->ui->extendedit->append (Subsc3.property ("key"). ToString () + ":"); This->ui->extendedit->append (Subsc3.property ("value"). toString ());}
Results