Titanium mobile解析json响应

Oli*_*ton 0 json titanium titanium-mobile

我试图从我的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":" "},
Run Code Online (Sandbox Code Playgroud)

我的钛码是

var cats = eval('('+this.responseText+')');
alert(cats[0]);
Run Code Online (Sandbox Code Playgroud)

这得到我'Foo.com特价'然而我需要DishName,任何帮助将非常感谢谢谢

Ren*_*Pot 5

实际上,您将获得JSON字符串,而不是JSON对象.有一个内置功能可以将JSON字符串解析为JSON对象:

var response = JSON.parse(this.responseText);
Run Code Online (Sandbox Code Playgroud)

然后获取DishName很容易:

var dishname = response[0].DishName;
Run Code Online (Sandbox Code Playgroud)

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