0

am fetching data from php as json

CODE:

I have added json_encode() to the php database output

JQUERY MOBILE:

$.getJSON('custom/php/showresults.php',
 function(data){
console.log(data)
}

The console.log(data) outputs as:

{'name':'Geowan', 'surname':'hiu'}.

How can i change it to output in the object way so that i can access the data using data.name

1
  • 2
    it's json, and you fetched it via .getJSON, have you simply tried data.name? Commented Apr 20, 2016 at 19:55

2 Answers 2

1

do you want to just access the data straight?

$.getJSON('custom/php/showresults.php',
 function(data){
 console.log(data.name)
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want it to assign it to a variable

var jsonData = $.getJSON('custom/php/showresults.php',
 function(data){
 return data;
}
if(jsonData){
console.log(jsonData.whatever)
}

or simply

$.getJSON('custom/php/showresults.php',function(data){
    return data.whatever;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.