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
gpatton
Posts: 193
Registered: ‎12-29-2010
My Device: Bold 9900
My Carrier: Rogers
Accepted Solution

Doing all of JSON from QML

Hi,

 

Just trying to decide what type of model to use for a list I wanted to do.

Is it true that I can't fully implement JSON in its entirety in QML? i.e. sorting keys (and everything else 100%) in QML, no C++ at all. (I have a JSON file that I've created)

 

From the examples I see, there needs to be some minimal Implementation in C++ in addition to QML.

 

Alternatively I can use XML purely in QML but won't get the features of sorting etc. 

 

Is this all correct?

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Doing all of JSON from QML

I'm unclear on exactly what you're trying to do, but I'll note a few things.

1. JSON is a serialization format, not really a data structure that one uses and manipulates in-memory. That's a bit like saying XML is not the same as the DOM which is often how one manipulates the same data after reading it in or before writing it out.

2. QML can use Javascript, pretty much all of it. If you can parse the JSON data, you should be able to get it into basic Javascript data structures, starting with an object (dictionary/mapping/whatever you want to call it) or an array (list/whatever) for the outer data structure, and other things nested within it.

I do not, however, know the best mechanism for getting JSON data off the filesystem and into your QML (Javascript). There is some C++ support for it, but nothing native to Javascript unless you can just run eval() on it (which, given JSON's origins as a Javascript Object Notation, is quite possible...)

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
gpatton
Posts: 193
Registered: ‎12-29-2010
My Device: Bold 9900
My Carrier: Rogers

Re: Doing all of JSON from QML

Hi,

 

Thanks for the reply. I guess i"m referring to this document: (https://developer.blackberry.com/cascades/documentation/ui/lists/groupdatamodel.html) I saw this line in it: 

 

By using C++, you can access data from various sources more easily, such as from a SQLite database or JSON data structure.

 

I guess it's implied that you can't use JSON in QML, from what I read?

 

What I'm trying to do is have a list of items, let's say "Fruits". There are apples, oranges, grapes etc and each one of them has a property...size, color etc. I want to be able to view this data in a list by the "Type" property. And also have the ability to sort the items in the list alphabeticaly/numerically (Type or Color or Size). This would be acheived by pressing one of the Action items on the ActionBar.

 

I'm trying to steer away from having to deal between QML and C++ and want to do everything in my QML file. The file fruits.json is below:

 

[
	{
		"Number": "1",
		"Type": "Apple",
		"Color": "Red",
		"Size": "6"
	},
	{
		"Number": "2",
		"Type": "Orange",
		"Color": "Orange",
		"Size": "10"
	},
	{
		"Number": "3",
		"Type": "Grape",
		"Color": "Green",
		"Size": "1"
	}
]

 

 

Please use plain text.
Developer
soaman
Posts: 848
Registered: ‎03-03-2011
My Device: Z10

Re: Doing all of JSON from QML

[ Edited ]

Use JavaScript in QML to move away from C++..

--------------------------------------------------------------------------------------
Leader of Ljubljana BlackBerry Developer Group
BlackBerry Certified Builder for Native Application Development
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Doing all of JSON from QML

I just noticed tonight that the Javascript "global" object, in the Cascades QML environment, has a JSON object in it.

 

This appears to be the ECMAscript 5 standard JSON object which is documented here among other places: https://developer.mozilla.org/en-US/docs/Using_native_JSON

 

Using that, you could do JSON.parse(somestring) and get back the output of parsing the JSON.  Similar to eval() but no doubt better...

 

Also has a JSON.stringify() to go the other direction.

 

These things were doubtless obvious to many who are reading this... was news to me though.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
ekke
Posts: 1,428
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: Doing all of JSON from QML

Peter,

 

what do you mean by JavaScript "global" Object ?

and HowTo use it in QML ?

import something ?

 

I'm also looking to find out HowTo do basic JavaScript functionality inside QML like substring(), length etc

 

thx

 

ekke

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

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

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Doing all of JSON from QML

Ekke, the "global object" refers to a sometimes-invisible object which is always in scope, and which provides all the names which are available without having to import or do anything.  For example, you can always call parseInt(), or you can always "throw new Error('foo')" because parseInt and Error are both in the global object.

 

In a browser this is normally called "window", but in an environment like this there may be no direct way to get to the global object to inspect it.  You can, however use this handy routine (as I found out): http://www.nczonline.net/blog/2008/04/20/get-the-javascript-global/

 

With that function, you could assign the return value to, say "g", and do console.log(Object.getOwnPropertyNames(g)) or something like that (notice that both "console" and "Object" are available there because they too are in the global object), and you'd see this list:

 

  • Math
  • NaN
  • undefined
  • Infinity
  • JSON
  • Object
  • Function
  • Array
  • Boolean
  • String
  • Number
  • Date
  • RegExp
  • Error
  • EvalError
  • RangeError
  • ReferenceError
  • SyntaxError
  • TypeError
  • URIError
  • eval
  • parseInt
  • parseFloat
  • isNaN
  • isFinite
  • escape
  • unescape
  • decodeURI
  • decodeURIComponent
  • encodeURI
  • encodeURIComponent
  • print
  • gc
  • Qt
  • XMLHttpRequest
  • DOMException
  • openDatabaseSync
  • SQLException
  • console
  • qsTranslate
  • QT_TRANSLATE_NOOP
  • qsTr
  • QT_TR_NOOP
  • qsTrId
  • QT_TRID_NOOP

Unfortunately it's incomplete, and I don't know why yet.  At the very least, "Application" exists there, providing the ability to do "Application.quit()" from within the Javascript, but I haven't yet found either documentation nor a technique that would let one discover that as with the above.


As for substring() and such, Javascript has several -- substr(), substring(), and slice() -- depending on your goals. These sorts of things should all be standard and in any Javascript/ECMAscript documentation that covers the correct version. (Which version? I'm not 100% clear on that yet...)  We should start new threads for further discussion on that.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
ekke
Posts: 1,428
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: Doing all of JSON from QML

Peter,

cool what you found out - I like'd it ;-)

starting a new thread is a good idea

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

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

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
BlackBerry Development Advisor (Retired)
gperry
Posts: 138
Registered: ‎05-11-2012
My Device: Developer
My Carrier: Developer

Re: Doing all of JSON from QML

If another thread is starting - is it OK to finish this one off?

 

Graham

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Doing all of JSON from QML

I'm not the OP, but I wouldn't close this one just yet. gpatton hasn't responded to my suggestion of using the JSON.parse() routine yet, and whether he can turn that into a solution for his original problem.

Our new threads (two now) are just to take the more general discussion out of here.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.