Titanium mobile parse json response

Go To StackoverFlow.com


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+')');

This get's me 'Foo.com Specials' however I need the DishName, any help would be much appreciated Thanks

2012-04-04 19:50
by Oliver Bayes-Shelton
Please be sure to mark one of the replies as correct - flumpb 2012-04-05 19:36


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.

2012-04-04 22:55
by Rene Pot
Thank you very much. Really needed that part about how to get the dishname - James 2012-05-16 15:52


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);     
2012-04-05 04:26
by Maulik