Titanium Community Questions & Answer Archive

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

is this a legit JSON file... and...

how do I pull data out of it:

{"Items":
[{"STATE":
{"Title":"US-CALIF-GEN","LOCATION-tax":"Anaheim\/LA","Pin Free Access Numbers":{
"1":"510-431-0037",
"2":"415-226-9960"}},
"label":"Item",
"type":"STATE"},
{"STATE":
{"Title":"US-CALIF-ANAHEIM\/LA","LOCATION-tax":"Anaheim\/LA","Pin Free Access Numbers":{
"1":"213-455-0203",
"2":"310-437-8924",
"3":"310-584-8932",
"4":"323-212-9942","5":"562-391-0055","6":"714-249-9929","7":"714-463-9939","8":"760-561-0033","9":"818-230-0054","10":"858-384-9926","11":"909-586-9941","12":"916-265-9933","13":"949-544-0058","14":"951-271-9929","15":"626-551-0058 "}},"label":"Item","type":"STATE"},{"STATE":{"Title":"US-CALIFORNIA-LOSANGELES","LOCATION-tax":"Los Angeles","Pin Free Access Numbers":{"1":"818-230-0054","2":"213-455-0203"}},"label":"Item","type":"STATE"}]}

I added some line breaks at the beginning just to make it more readable.

two questions.
first, is this a valid JSON. It seems to pass the JSON smel test at http://www.jsonlint.com/. But I am having troubles getting data out of it.

Second Question.
I am using the this code to pull data out of it… I think I am doing something wrong.. perhaps some one can correct me please. and some pointers on how to get at the arrays?

b7.addEventListener('click', function()
{

           Ti.API.info('=====   about to read file=============');
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'accessnumbers.json');

// read the file into contents var

var contents = f.read();

// The Next line works... and does print out contents of file 
Ti.API.info('contents = ' + contents.text);    

// this is where things are not working...
var resources = JSON.parse(f.read().text);

Titanium.API.info('======   starting up tf1 ================');
Titanium.API.info('=========================================');
// I am trying out different combinations to get to the data in this file. 
var vv = resources.Items.STATE.Title;
Titanium.API.info(vv);
//var vv = resources.[Items].[STATE];
Titanium.API.info(vv);
var vv = resources[Items][STATE];
Titanium.API.info(vv);
var vv = resources.Items;
Titanium.API.info(vv);

returns null. or crashes.
So, what am I doing wrong if I want to get data out of one of those arrays?

— asked September 9th 2010 by vincent youmans
  • json.parse
0 Comments

2 Answers

  • "Items" is an array

    try

    var vv = resources.Items[0].STATE.Title;
    Titanium.API.info(vv);
    
    var vv = resources.Items[1].STATE.Title;
    Titanium.API.info(vv);
    
    — answered September 9th 2010 by Dan Tamas
    permalink
    0 Comments
  • or even better, loop through the Items array and output the STATE.Title

    for(var i in resources.Items){
       Ti.API.info(resources.Items[i].STATE.Title);
    }
    
    — answered September 14th 2010 by Sam Shupac
    permalink
    0 Comments
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.