Titanium - 无法解析本地JSON文件

时间:2012-04-30 22:35:06

标签: json parsing titanium local titanium-mobile

我正在尝试解析存储在applicationDataDirectory中的本地JSON文件。我只需要从这个文件中获取项目。运行钛的V.2

JSON数据结构如下所示

{
"object_name": [
    {
        "title": "Burrton",
        "value": "Burrton",
        "homeof": "Chargers",
        "logo": "images/logos/BURRTON.jpg",
        "colors": "#cccccc;#a9a9a9",
        "links": {
            "tsr": "http://www.schurzdigital.com/mfeeds/CollectionFeed.php?site=http%3A%2F%2Fwww.catchitkansas.com&collection=cik_burrton_headlines&title=cik_burrton_headlines&limit=25",
            "sports": "www.schurzdigital.com/mfeeds/CollectionFeed.php?site=http%3A%2F%2Fwww.catchitkansas.com&collection=cik_burden_headlines&title=cik_burden_headlines&limit=25",
            "videos": "http://www.schurzdigital.com/mfeeds/TividFeedConvert.php?site=http%3A%2F%2Fwww.catchitkansas.com&slug=e9c0b075-3230-4a45-803e-7ccb4b7f754e&title=Top%20videos&limit=25",
            "photos": "http://serve.a-feed.com/service/getFeed.kickAction?feedId=1014509&as=8768",
            "stats": {
                "boys": {
                    "football": "http://stats.catchitkansas.com/report_fb-schedule_results.php?sport=15&team=763",
                    "basketball": "http://stats.catchitkansas.com/report_baskb-schedule_results.php?sport=26&team=2946",
                    "cross country": "http://stats.catchitkansas.com/report_cc-schedule_results.php?sport=13&team=764",
                    "golf": "http://stats.catchitkansas.com/report_golf-schedule_results.php?sport=16&team=767",
                    "track": "http://stats.catchitkansas.com/report_trackfield-schedule_results.php?sport=32&team=2948"
                },
                "girls": {
                    "volleyball": "http://stats.catchitkansas.com/report_vb-schedule_results.php?sport=22&team=766",
                    "basketball": "http://stats.catchitkansas.com/report_baskb-schedule_results.php?sport=27&team=2947",
                    "cross country": "http://stats.catchitkansas.com/report_cc-schedule_results.php?sport=13&team=764",
                    "golf": "http://stats.catchitkansas.com/report_golf-schedule_results.php?sport=17&team=768",
                    "track": "http://stats.catchitkansas.com/report_trackfield-schedule_results.php?sport=32&team=2948"
                }
            }
        }
    }
]
}

我正在使用的代码来解析文件。

var fileName = 'school_select.json';
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, fileName);

if (file.exists()) {
  var json = file.read();
  var jsonObject = JSON.parse(json);

    one.text = jsonObject.object_name[0].title; 
    two.text = jsonObject.object_name[0].homeof;
}

1 个答案:

答案 0 :(得分:5)

我回答了自己的问题。我使用了以下代码。

var fileName = 'school_select.json'; 
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, fileName);    
var json, object_name, locatie, i, row, title, value, homeof, logo, colors, link; 
var preParseData = (file.read().text); 
var response = JSON.parse(preParseData); 
    Ti.API.info('title = ' + response.object_name[0].title);