JSON Nested Object Help
I'm pulling in JSON from a Drupal site which is working fine. The issue I'm having is with nested objects. The field_image consists of an array and I need to pull the filename from it.
In the below code, how can I use the nested object?
var label2 = Ti.UI.createLabel({ text: 'Image: '+data[c]['field_image'] });
JSON output
[field_image] => Array
(
[0] => Array
(
[uid] => 1
[filename] => FeedsEnclosure-ClassAd-297081-1.jpg
)
)
5 Answers
-
sorry should have put that code in correctly
[field_origin] => Array ( [0] => Array ( [value] => Edmonton, Alberta, Canada ) )
-
untested, try this for size.
var data = JSON.parse(this.responseText);
value = data.field_origin[c].value;
-
Thanks Daniel. Unfortunately that did not work for me and I was typing out a long reply explaining what I was trying to do and how I was trying to do it and then I thought to try something and it worked!!!
Seeing as there is only one value in these arrays returned by Drupals Services module it is fairly safe to assume that each position in the array is going to be [0]
I was able to set the label to this value within the array by using the following
text: data[c]['field_origin'][0]['value'], -
I figured out a simple way to avoid Drupal outputting CCk and taxonomy fields in arrays which makes it tricker to access the relevant values. If you use "Row Style:Fields" instead of "Row Style: Node" then you get all the values at a top level without being nested in arrays which makes them easy to pull through to Titanium.
Hope that helps someone because it frustrated me for a while :)
-
Gregg's example works for all my fields except taxonomy and image fields. As an example: when I use data[c]['field_image'][0]['filepath'], the page loads blank.