Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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  
                        )
                )
— asked June 23rd 2010 by Jason Calvert
  • iphone
  • json
1 Comment
  • I am also looking for the answer to this exact question.
    In this piece of code
    [field_origin] => Array
    (
    [0] => Array
    (
    [value] => Edmonton, Alberta, Canada
    )

                )
    

    I would like to access the [value] field
    I have been using data[c] field to access top level elements like [field_origin] but need to dive a bit deeper into the array. Has anyone got any suggestions or did you ever manage to resolve this Jason?

    Thanks in advance
    Gregg

    — commented July 20th 2010 by Gregg Coppen

5 Answers

  • sorry should have put that code in correctly

     [field_origin] => Array
                    (
                        [0] => Array
                            (
                                [value] => Edmonton, Alberta, Canada
                            )
    
                    )
    
    — answered July 20th 2010 by Gregg Coppen
    permalink
    0 Comments
  • untested, try this for size.

    var data = JSON.parse(this.responseText);

    value = data.field_origin[c].value;

    — answered July 20th 2010 by Daniel Lim
    permalink
    0 Comments
  • 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'],

    — answered July 20th 2010 by Gregg Coppen
    permalink
    1 Comment
    • Great job Gregg! I had a new project come up and had to put my app aside for a while. Haven't had a chance to work on this issue. Glad you got it to work.

      — commented July 20th 2010 by Jason Calvert
  • 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 :)

    — answered July 21st 2010 by Gregg Coppen
    permalink
    0 Comments
  • 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.

    — answered July 21st 2010 by Jason Calvert
    permalink
    2 Comments
    • are you using field or node rowstyle for your view? If using node try using fields and then you can access most elements as top level rather than nested. It worked for me for taxonomy.

      — commented July 22nd 2010 by Gregg Coppen
    • I've tried it as field but it doesn't show all the fields just the basic ones (teaser, title, etc.). The only way I can see all of the fields is by using rowstyle node.

      — commented July 29th 2010 by Jason Calvert
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.