I'm stumped.
When a user logs into my app successfully, I store a Profile object in the settings like this, and this is the return value. {"id":"7","email":"email@yahoo.com","first_name":"Test","last_name":"User", "group_id":"0","screen_name":"testie","profile":[],"member_since":"09-19-2011","social":""}
Ti.App.Properties.setString('profile',JSON.stringify(returnvar));That's all fine and dandy, and in a few file I get the values like this.
var p = JSON.parse(Ti.App.Properties.getString('profile'));BUT there are times when I try to access by executing the above code and I get undefined, when I try to do something like
p.email
Does anybody see anything wrong with this JSON string? I use this approach for several things, but the particular string is causing me issues.
thanks
3 Answers
put some
Ti.API.info(p)to see what it gets from properties, assuming it stores it ok first.
separate every operation u make and put an info to see what is going on after each of them.
Hi Matt
I have checked your JSON returnvar in JSONlint.com and you have not issues. SO I duplicated your code and made my own returnvar from your example;
It works perfect every time - I get 'email@yahoo.com' in the alert.
So either you have a problem before the returnvar is processed or it is one of those times to use Projects > Clean. Because your code looks solid.
var returnvar = { "id":"7", "email":"email@yahoo.com", "first_name": "Test", "last_name":"User", "group_id":"0", "screen_name":"testie", "profile":[], "member_since": "09-19-2011" ,"social":"" }; Ti.App.Properties.setString('profile',JSON.stringify(returnvar)); var p = JSON.parse(Ti.App.Properties.getString('profile')); alert(p.email);
Just looking at your code, everything looks good. Remember that JSON keys are case sensitive. We use a Coldfusion REST API and unless I specify the key as a string in lowercase, it gets converted to uppercase. This was a headache for me at first. I know you wrote your keys in lower case, but make sure they actually are lower case. p.EMAIL in stead of p.email.
Other than that you code "looks" ok.
You can try:
alert(JSON.parse(Ti.App.Properties.getString('profile')).email);just to see if something is happening strange in your assigning of variables or something. Try just alerting out the entire object either like this:
alert(JSON.parse(Ti.App.Properties.getString('profile')));or like this
alert(Ti.App.Properties.getString('profile'));just to make sure you are indeed storing the value in the properties. Also make sure you are not accidentally clearing or properties in some sort of init function or something. I've done that more than once.
Your Answer
Think you can help? Login to answer this question!