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: 26
Registered: ‎07-22-2014
My Device: Z30, Z10, Q10, Passport
My Carrier: Telus
Accepted Solution

Read value from XML or JSON based on key name

I can't figure out how to get the url value for Image_Calendar1 from either the XML or the JSON data.  Sorry if it's "obvious" but I'm a new developer.  

 

I've been cheating and using ListItemData.value[1].url but in this case the XML feed changes the number of values some times which throws off my method.  I need to be able to get the value based on the key name.  Is that possible? 

 

Thanks. Here are samples of the XML and the correspnding JSON. 

 

<data serverTime="--------------" offset="-240">
<script id="tinyhippos-injected"/>
<row recordDate="--------------" updateDate="--------------" rowId="--------------">
<value key="template">TableRow_CalendarDays</value>
</row>
<row recordDate="--------------" updateDate="--------------" rowId="--------------">
<value key="template">TableRow_Calendar</value>
<value key="Image_Calendar1">
<url>--------------</url>
</value>
<value key="Label_Team1">
<text/>
</value>
<value key="Label_Score1">
<text/>
</value>
<value key="Label_Status1">
<text/>
</value>
<value key="Label_Calendar1">
<text/>
</value>
<value key="Label_GameId1">
<text/>
</value>
<value key="Image_Calendar2">
<url>--------------</url>
</value>
<value key="Label_Team2">
<text/>
</value>
<value key="Label_Score2">
<text/>
</value>
<value key="Label_Status2">
<text/>
</value>
<value key="Label_Calendar2">
<text/>
</value>
<value key="Label_GameId2">
<text/>
</value>

 

{"recordDate":"--------------","rowId":"--------------","updateDate":"--------------","value":[{".data":"TableRow_Calendar","key":"template"},{"key":"Image_Calendar1","url":"--------------"},{"key":"Label_Calendar1","text":"10"},{"key":"Label_Team1","text":""},{"key":"Label_Score1","text":""},{"key":"Label_Status1","text":""},{"key":"Label_GameId1","text":""},{"key":"Image_Calendar2","url":"--------------"},{"key":"Label_Calendar2","text":"11"},{"key":"Label_Team2","text"...

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Read value from XML or JSON based on key name

There are a number of samples that read XML data my recommendation would be to look at those...

 

Samples can be found here...

http://developer.blackberry.com/native/sampleapps/

 

You might also find this useful...

 

http://developer.blackberry.com/native/documentation/cascades/device_platform/data_access/working_wi...


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

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Read value from XML or JSON based on key name

Same for JSON...

 

http://developer.blackberry.com/native/documentation/cascades/device_platform/data_access/working_wi...


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

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 358
Registered: ‎04-13-2013
My Device: Z10
My Carrier: Telus

Re: Read value from XML or JSON based on key name

[ Edited ]

for JSON you do something like this:

 

Import bb.data.1.0

DataSource{
id: feedDataSource
source: #where you stick your JSON URL feed#

onDataLoaded{

recrodDateLabel.text = data.recordDate

}

}

Label{
id: recordDateLabel

}

 Every time there's a new parent you have to do "data.parent.child"  if there's no parent object you can just do "data.child"

Developer
Posts: 26
Registered: ‎07-22-2014
My Device: Z30, Z10, Q10, Passport
My Carrier: Telus

Re: Read value from XML or JSON based on key name

Thanks for the tips guys.  I don't think that's quite what I was looking for though.  I can count the number of elements in the root of the XML but I need to be able to count the number of child elements.  

 

For example, this feed has Image_Calendar2, Label_Team2, Label_Score2, Label_Status2, Label_Calendar2, Label_GameId2 for child elements in the second row elements, but sometime there are more, some times there are less.  I need to be able to return the count/size/length of that child collection so that I know how many time to cycle through while parsing.   

 

Alternatively, is there a way to do a for loop in QML and tell it to continue until the end of the XML? 

Developer
Posts: 26
Registered: ‎07-22-2014
My Device: Z30, Z10, Q10, Passport
My Carrier: Telus

Re: Read value from XML or JSON based on key name

OK, it might not be pretty but what I'm doing to fix my issue is convert the ListItemData to JSON and stringify it.  I then split the string by "key" and then count the lines.  Wouldn't work if "key" wasn't in each line, or if there were child elements of the child elements.  But... it works for me for now.  Future me will hopefully be smarter and can fix it when it breaks. 

 

                                        var testString = String(JSON.stringify(ListItemData))

                                        var javascriptcount = String(testString.split("key").length - 1)

                                        console.log('trying to use length: ' + javascriptcount)