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
Highlighted
Developer
Posts: 135
Registered: ‎04-25-2009
My Device: Z30
My Carrier: AT&T

How can I access children of JSON data simply?

I have json data that I receive over the network. However, I do not want to put it in a list. All I want is to easily be able to grab a hold of the value of one of the children. Seems very simple, but after hours and hours of searching I have found nothing.

 

void myApp::loginRequestFinished(QNetworkReply* reply) {
	if (reply->error() == QNetworkReply::NoError) {

		const QByteArray response(reply->readAll());
		QString stringResponse = QString(response);
		// and parse the JSON into a usable format
		JsonDataAccess jda;
		QVariantMap results = jda.loadFromBuffer(response).toMap();

//**********//
} else { qDebug() << "\n Problem with the network"; qDebug() << "\n" << reply->errorString(); } reply->deleteLater(); }

 

As you can see I put my http response into a QByteArray and then into a QVariantMap. I also made a String type of the data so I could easily see the response while debugging.

From this point, what variable type do I have to assign the json data to in order to grab certain values. For instance, my JSON data looks like this:

 

{
  "json": {
    "errors": [],
    "data": {
      "modhash": "modhashRandom45866392025685",
      "cookie": "cookierandom348539943853989453"
    }
  }
}

 

And I just want to grab the string value of the modhash and the cookie, and then do things with them.

I'm looking for something like the following:

QString modhashValue = jsonDataVariable.data.modhash;

 

Just grabbing the value of json properties. Very simple, but how?

 

Thanks!

 

 

 

Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: How can I access children of JSON data simply?

https://developer.blackberry.com/cascades/reference/bb__data__jsondataaccess.html

 

just break it down:

 

QVariantMap results = jda.loadFromBuffer(response).toMap();

 

QVariantMap json = results["json"].toMap();

 

QVariantMap data = json["data"].toMap();

 

QString cookie = data["cookie"].toString();

 

 

Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: How can I access children of JSON data simply?

using it this way you can work with defaults if the key wasn't found in the map:

data.value("cookie", "chocolate").toString();

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner