Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
calicratis19
Posts: 163
Registered: ‎12-06-2012
My Device: Z10, Q10
Accepted Solution

QByteArray to Json

How to convert a QByteArray recieved from a network reply to JSON?

Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QByteArray to Json

Qt 5 has built-in parser, but in Qt 4.8 you'll have to use a third-party one. I'm using QtJson. It's very lightweight:

https://github.com/ereilin/qt-json

 

bool ok;
QVariantMap result = QtJson::Json::parse(byteArray, ok).toMap();
if (!ok)
{
  // report error
  return;
}
    
bool retval = result["retval"].toBool();
int status = result["status"].toInt();
QString token = result["token"].toString();
QVariantMap m = result["m"].toMap();
double d = result["d"].toDouble();

 

 


Andrey Fidrya, @zmeyc on twitter
Developer
SumiGhosh
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380

Re: QByteArray to Json

Check with the below function:

 

void GridSample::requestFinished( QNetworkReply * reply){
	QVariantMap dataName;
	        if (reply) {
	            if (reply->error() == QNetworkReply::NoError) {
	                const int available = reply->bytesAvailable();
	                if (available > 0) {
	                	const QByteArray buffer(reply->readAll());
	                	
	                    bb::data::JsonDataAccess ja;
	                    const QVariant jsonva = ja.loadFromBuffer(buffer);
	                    const QVariantMap jsonreply = jsonva.toMap();
	                    QVariantList data = jsonreply["photos"].toList();
	                    foreach(QVariant v, data){
	                    	dataName["text"]=v.toMap().value("created_at").toString();
	                    	myGroupModel->insert(dataName);
	                    }
	                }
	            } else {
	            	
	            }
	            reply->deleteLater();
	        }
}