1

I am using PHP and Ajax to parse some JSON. The PHP is creating the array like this.

$myObj->pid = $_POST["parentid"];
$myObj->comp = $comp;
$myObj->colour = $statuscolour;
$myJSON = json_encode($myObj);
header('Content-Type: application/json');
echo $myJSON;

I use the following jquery code

$.ajax({
        type: "POST",
        dataType: "json",
        url: "msstatup.php",
        data: data
    }).done(function(msg) {
    var response = jQuery.parseJSON(JSON.stringify(msg));
    console.log(response);
    pid = response[0].pid;
    console.log('id = ' + pid);
    });

I can see the output from the first console.log as

Object {pid: "p1", comp: 20, colour: "red"}

However I cannot extract the individual variables, it gives the message

Uncaught TypeError: Cannot read property 'pid' 

How can I extract the variable?

4
  • 1
    Since you're returning JSON you do not have to use parseJSON. Commented Feb 13, 2017 at 17:31
  • 2
    Why are you first stringifying then parsing the JSON response? Commented Feb 13, 2017 at 17:32
  • You seem to be treating the response as an array when it is a single object. Try console.log(response.pid) Commented Feb 13, 2017 at 17:33
  • 1
    msg.pid is your answer Commented Feb 13, 2017 at 17:34

3 Answers 3

4

You've made this more complicated than it needs to be. msg is already an object, which you then convert to a string and back to an object with stringify and parseJSON. And then you try to use it like an array, when it is an object.

Try this:

$.ajax({
    type: "POST",
    dataType: "json",
    url: "msstatup.php",
    data: data
}).done(function(msg) {
    var pid = msg.pid;
    console.log('id = ' + pid);
});
Sign up to request clarification or add additional context in comments.

Comments

2

You are returning an object, not an array.

Also it makes no sense to stringify the data object and parse that string back to object again

Try

var pid = msg.pid;
console.log('id = ' + pid);

Comments

0

First of all, it can't imagine why it would be neccessary to first stringify, then parse the JSON response.

Second, you are trying to access response[0] as if it were an array, which it isn't. You can simply use

response.pid;

To access the object key.

As you don't need to stringify then parse the response, you can just access msg directly, so it all comes down to

$.ajax({
    type: "POST",
    dataType: "json",
    url: "msstatup.php",
    data: data
}).done(function(msg) {
    console.log(msg.pid);
});

6 Comments

msg.pid There is no need for the whole line of code defining response.
I did not DV. But, if I were that guy I would have DV'd because the original end result was not correct or at least compensating for the line of code defining response
You mean the typo I had in the very first save of my answer (still leaving the [0]), that I corrected within seconds of posting?
No, response.pid. While correct technically it supported the over-complication of the code.
It still was a correct answer that maybe could have used some further tweaking. Imho that's a reason to upvote other answers that cover this better, not downvote an answer. Otherwise, where do we the line of uncovered code optimisations?
|

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.