I am currently using Phonegap and XUI to create web app.
I am retrieving some data from an external domain using a http request via XUI.
This is working correctly and I receive the JSON data back correctly, see the data format below:
({"first":"John","last":"Smith","HighScore":"75"})
So now I want to be able to access the individual assets of the data using javascript.
x$('#test').xhr(URL,function() {
loggedin = this.responseText; // This is the data that has been received from the PHP file
if(loggedin != '1') // If not 1 then will let them in
{
alert(loggedin); // Alerts with the data recieved
开发者_StackOverflow社区 }
else // Login incorrect
{alert('Sorry you login details were incorrect please try again.');}
});
I know its probably simple to do but I just can't seem to figure it out so any help would be much appreciated.
Thanks,
Kane
JSON object accessor syntax is object.key
, so if this.responseText
is {"first":"John","last":"Smith","HighScore":"75"}
then you'd display Smith
with this.responseText.last
An example usage for your alert could be:
alert('Hello ' + this.responseText.first + ' ' this.responseText.last + '! You currently have a high score of ' + this.responseText.HighScore + ' points! Play again!');
精彩评论