Reputation: 6292
I am trying to get the DishName from this json string which is being returned from my php api.
The json string is
["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":" "},
My titanium code is
var cats = eval('('+this.responseText+')');
alert(cats[0]);
This get's me 'Foo.com Specials' however I need the DishName, any help would be much appreciated Thanks
Upvotes: 0
Views: 2925
Reputation: 19418
First thing your JSON response is not valid. You can validate your JOSN string Online here.
You can parse your JSON response by built in method JSON.parse()
.
Sample code:-
yourLoader.onload = function()
{
var response = JSON.parse(this.responseText);
var dishname = response[0].DishName;
Ti.API.log('Your Dish Name:'+dishname);
}
Upvotes: 2
Reputation: 24815
You will actually get back a JSON string, not a JSON object. There is a build in feature for parsing the JSON string to JSON object:
var response = JSON.parse(this.responseText);
Getting the DishName then is easy:
var dishname = response[0].DishName;
Note: Your currently displayed JSON seems to be incomplete, or otherwise it is an invalid JSON object.
Upvotes: 5