0

我正在尝试从我的 php api 返回的这个 json 字符串中获取 DishName。

json字符串是

["Spicy.com Specials",{"CatID":31,"CatName":"Spicy.com Specials","DishName":"Kashmiri Chicken","DishID":52,"DishDesc":"Cooked with lychees and banana in a lovely sweet and creamy sauce","DishPrice":6.99,"CatDescription":" "},{"CatID":31,"CatName":"Spicy.com Specials","DishName":"Telapia Fish","DishID":51,"DishDesc":"Lightly spiced fillet, a very popular white fish made with peppers, onions and spices in medium sauce","DishPrice":6.99,"CatDescription":" "},

我的钛代码是

var cats = eval('('+this.responseText+')');
alert(cats[0]);

这得到了我的“Foo.com Specials”但是我需要 DishName,任何帮助将不胜感激谢谢

4

2 回答 2

5

您实际上会得到一个 JSON 字符串,而不是 JSON 对象。有一个用于将 JSON 字符串解析为 JSON 对象的内置功能:

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

然后获取 DishName 很容易:

var dishname = response[0].DishName;

注意:您当前显示的 JSON 似乎不完整,否则它是无效的 JSON 对象。

于 2012-04-04T22:55:50.300 回答
2

首先,您的 JSON 响应无效。您可以在此处在线验证您的 JOSN 字符串。

您可以通过内置方法解析您的 JSON 响应JSON.parse()

示例代码:-

yourLoader.onload = function()
{
    var response = JSON.parse(this.responseText);
    var dishname = response[0].DishName;

    Ti.API.log('Your Dish Name:'+dishname);     
}
于 2012-04-05T04:26:59.320 回答