我如何在Titanium Mobile App中解析此JSON提要

时间:2011-09-06 06:13:50

标签: titanium titanium-mobile

如何在Titanium Mobile App中解析此JSON Feed。

{
    "data":{
        "mat_78":{
            "id":"78",
            "title":"latest title here",
            "teaser":"latest teaser here",
            "body":"latest body here",
            "logo":"http://sites/default/files/6_0.jpg",
            "files":{
                "323":{
                    "fid":"323",
                    "filename":"joudi1m.jpg",
                    "tumb":"http://sites/default/files/joudi1m_0.jpg",
                    "large":"http://sites/default/files/joudi1_0.JPG",
                    "caption":"joudi1m.jpg"
                },
                "471":{
                    "fid":"471",
                    "filename":"Shot-(0_00_15_09)m.jpg",
                    "tumb":"http://sites/default/files/Shot(0_00_15_09)m.jpg",
                    "large":"http://sites/default/files/Shot(0_00_15_09).jpg",
                    "caption":"Shot(0_00_15_09)m.jpg"
                }
            }
        },
        "mat_79":{
            "id":"79",
            "title":"latest title 2 here",
            "teaser":"latest teaser 2 here",
            "body":"latest body 2 here",
            "logo":"http://sites/default/files/d09a0498a8512.jpg",
            "files":{
                "339":{
                    "fid":"339",
                    "filename":"BO1m.png",
                    "tumb":"http://sites/default/files/BO1_0.png",
                    "large":"http://sites/default/files/BO4_0.PNG",
                    "caption":"BO1m.png"
                },
                "340":{
                    "fid":"563",
                    "filename":"Slide9m.jpg",
                    "tumb":"http://sites/default/files/Slide9m.jpg",
                    "large":"http://sites/default/files/Slide9.JPG",
                    "caption":"Slide9m.jpg"
                }
            }
        },
        "mat_link":"mysite.cim/"
    }

这是我解析JSON的代码段。不要......它给我'未定义'

var loader = Titanium.Network.createHTTPClient();
    loader.open("GET","http://mysite.com/data_iphone/en&lmt=5");    
    loader.onload = function() 
    {
        var data = JSON.parse(this.responseData);
        Ti.API.debug(data); 
        for (var i = 0; i < data.length; i++)
        {
        var my_id = data[i].id; // id
        var my_title = data[i].title; // title
        Ti.API.debug(my_title); 
        }
    }

......
.........

2 个答案:

答案 0 :(得分:5)

我认为应该更像这样

    // NOTICE EXTRA "data" on end, if you are using JSON.parse()
    var data = JSON.parse(this.responseText).data;

    Ti.API.debug(data);

    for (var i = 0; i < data.length; i++)
    {
        var my_id = data[i].id; // id
        var my_title = data[i].title; // title
        Ti.API.debug(my_title); 
    }

答案 1 :(得分:0)

使用onload函数从服务器解析json

var loader = Titanium.Network.createHTTPClient();
var requstUrl = "your url";
loader.open("GET", requstUrl);
loader.onload = function() {


    var daysh = eval('(' + this.responseText + ')');
    var status = daysh.status;
    var jsonArry = daysh.locations;
    for (var i = 0; i < jsonArry.length; i++)
    {

        alert(i);

    }


};
loader.send();