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
Kaz32
Posts: 237
Registered: ‎11-24-2012
My Device: Blackberry 10 Dev Alpha
Accepted Solution

How to Check JSON Object Properties Available

[ Edited ]

Hi I have JSON Object from WebService.

How to check JSON Object available?

 

ex JSON :


{

name : "someName"

desc: "someDesc"

}

 

How to check that name available?

Because sometime name properties not available from the server

 

void MyClass::httpFinished(QNetworkReply* reply)
{
QString data = (QString) reply->readAll();
bb::data::JsonDataAccess jda;
QVariant variant = jda.loadFromBuffer(data);
QVariantMap variantMap = variant.value<QVariantMap>();
QString name = variantMap.value("name").toString();
}

 I want to hide Label component on my .qml if name not available from webservice.

 

 

Thanks

 

BlackBerry Development Advisor
Kareem_ELSayed
Posts: 81
Registered: ‎09-09-2013
My Device: BlackBerry Z10

Re: How to Check JSON Object Properties Available

[ Edited ]

Hello, You can use isNull() on the QVariant/QVariantMap object to verify if it's NULL. also there are IsEmpty function for strings.

for example:

if (!variant.isNull()){ //Not NULL
   QVariantMap variantMap = variant.toMap();
   if(variantMap["name"].isNULL()) { //skip }
   //or 
   if(variantMap["name"].toString().isEmpty()) { //skip }
}

 


Thanks,
Kareem.

Kareem ELSayed | Application Development Consultant | Developer Relations | BlackBerry | Twitter:@kemobyte
Developer
Kaz32
Posts: 237
Registered: ‎11-24-2012
My Device: Blackberry 10 Dev Alpha

Re: How to Check JSON Object Properties Available

Hi Kareem,

 

Thanks for your answer, I think your suggestion will work.

 

I have another question, is that posible to check this on qml side? How?

ex:

Label {
text: ListItemData.name
visible: isVisible()

function isVisible() {
var isVis = true;

// Is that posible to check ListItemData.name from qml?
// What I do now is just check the length of ListItemData.name
// But sometime the JSON properties "name" it self not available
// 

return isVis;
}
}

 

 

 

Thanks

Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: How to Check JSON Object Properties Available


Kaz32 wrote:

Hi I have JSON Object from WebService.

How to check JSON Object available?

 

ex JSON :


{

name : "someName"

desc: "someDesc"

}

 

How to check that name available?

 

Thanks

 


If you want to check if name is available, you should use

 

bool nameExists = variantMap.contains("name")

 

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

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: How to Check JSON Object Properties Available

[ Edited ]

According to the docs, just ekke's line of code should be sufficient as toMap() returns an empty map if the data does not exist not null.

 

For the DataAccess you should check for an error rather than null...

 

QVariant loadFromBuffer (

Parses the specified JSON byte array buffer and returns it as a hierarchy of Qt value objects.

The data returned is a QVariant with a type that matches the type of the root JSON object (see the table of types in the class description of JsonDataAccess). Unless the data is a single primitive JSON value, the returned data will be either a QVariantList (if the root JSON node is an array) or a QVariantMap (if root JSON node is an object), cast as a QVariant.

You can use DataAccess::hasError() to determine if the most recent operation resulted in an error, and you can use DataAccess::error() to retrieve information about the error that occurred.

 

 

Kareem can you validate this as the docs are a bit vague on the scenario where the file and/or buffer is there but is totally empty, error or null in this case?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
BlackBerry Development Advisor
Kareem_ELSayed
Posts: 81
Registered: ‎09-09-2013
My Device: BlackBerry Z10

Re: How to Check JSON Object Properties Available

[ Edited ]

Hello 

QVariant m_JSONReturnObj = jda.loadFromBuffer(str);
bool result = m_JSONReturnObj.isNull();

Hope this help. Let me know if you have any comments / questions.

 

Thanks

Kareem.

 

Kareem ELSayed | Application Development Consultant | Developer Relations | BlackBerry | Twitter:@kemobyte
BlackBerry Development Advisor
Kareem_ELSayed
Posts: 81
Registered: ‎09-09-2013
My Device: BlackBerry Z10

Re: How to Check JSON Object Properties Available

I can add that

if the file is empty and JSONDataAccess return error then that how the Qvariant looks like >> QVariant(, ) [NULL]

but when the file is empty and JSONDataAccess doesn't return error then that how the QVariant Looks like >> QVariant(QVariantList, () ) [Not NULL but Empty list] it could be Qvarlist or qmap etc.

Thanks,
Kareem.
Kareem ELSayed | Application Development Consultant | Developer Relations | BlackBerry | Twitter:@kemobyte
Developer
Kaz32
Posts: 237
Registered: ‎11-24-2012
My Device: Blackberry 10 Dev Alpha

Re: How to Check JSON Object Properties Available

Hi, I'm using ekke sugestion and it work.

 

bool nameExists = variantMap.contains("name")

 

Thanks