0

No one really answered this question but how on earth can one use this JSON return data from a php/mysql direct using JavaScript?

Here is the return data once i used JSON.parse and saved it to the Javascript variable obj

[{"stuid":"10-00002","stuname":"Meratis, Velino","stucourse":"Arts","stustat":"0","stulyear":"4","stulog":"feb 16 2017"},{"stuid":"10-00003","stuname":"Melker, Alana","stucourse":"Wilderness","stustat":"1","stulyear":"5","stulog":"feb 16 2017"}]

I've tried the simple obj.stuname but it returns only an undefined i've tried many times to understand it but i can't seem to use this array at all. Could anyone help on this?

I've also tried the reObj = {"stu":obj}; style but then it only returns an [object Object]

so please someone elaborate on this?

1
  • how you get from php, share your ajax request code also ? Commented Feb 17, 2017 at 8:25

3 Answers 3

5

obj is a json array, so you have to access an element using its index.

Also, you have to use JSON.parse in order to turn a string of json text to a Javascript object.

Try this:

var stuname=obj[0].stuname;

var obj='[{"stuid":"10-00002","stuname":"Meratis, Velino","stucourse":"Arts","stustat":"0","stulyear":"4","stulog":"feb 16 2017"},{"stuid":"10-00003","stuname":"Melker, Alana","stucourse":"Wilderness","stustat":"1","stulyear":"5","stulog":"feb 16 2017"}]';
var objParsed=JSON.parse(obj);
console.log(objParsed[0].stuname);

If you want to iterate array, use forEach method.

var obj='[{"stuid":"10-00002","stuname":"Meratis, Velino","stucourse":"Arts","stustat":"0","stulyear":"4","stulog":"feb 16 2017"},{"stuid":"10-00003","stuname":"Melker, Alana","stucourse":"Wilderness","stustat":"1","stulyear":"5","stulog":"feb 16 2017"}]';
var objParsed=JSON.parse(obj);
objParsed.forEach(function(item){
  console.log(item.stuname);
});

Sign up to request clarification or add additional context in comments.

3 Comments

in addition i am using JSON encode from a php and send it via Json to a Javascript but it just says undefined when the data retrieved was perfect
thanks dude this helped a lot. but how do you use it on a for loop?
You means when I have a JSON string ?
1
  1. If you are getting this response from php via ajax. Be sure to use dataType as json to get json type response not string.

  2. Otherwise you need to parse json data like this

    obj = JSON.parse(jsonStrFromPhp);
    

Then you can fetch data as obj.stuname or obj[0].stuname depends how you returned from php like this {"stu":obj} or like this [{"stu":obj}]

1 Comment

i got the second one. the [{}] from my php. i can't seem to directly parse it coz if i only parse it, it just returns a [object Object],[object Object] so i had to stringify it then parse it afterwards to get my response. but it turned out to solve my problems
0

while i was having a migraine... of why my code was wrong it turned out that even though the obj = JSON.parse(jsonStrFromPhp); returned only an [object Object],[object Object] javascript can actually understand that shins and returned my variable... how confusing.

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.