RJDO
RJDO

Reputation: 65

How do I call a JSON object?

I am having a problem calling a specific object. For example, I want to call mydata.title

I have the ff inside my php (this is just part of the code)

while($row = $DB->result->fetch_assoc()){

        $Fthis[] = array( 

            "title" => $row["title"],
            "subtitle" => $row["subtitle"],
            "dates" => $row["dates"],
            "Author" => $row["Author"],
            "content" => $row["content"],
            "himgsrc" => $row["himgsrc"]
        );

          array_push($marxarray, $Fthis);
    }

    echo json_encode($marxarray);

And then I have this AJAX in my js.

$.ajax({
  url: 'editer.php',
  type: 'POST',
  data: { id: blogid },
  success: function (data) {
    var mydata= JSON.parse(data);
    console.log(mydata);
    alert(mydata.title);
  }
});

Why does the alert return undefined ?? If I look at the log, I can see that the array has been passed correctly so no problem with the php (i think).

here's how it looks like anyway.

enter image description here

Upvotes: 0

Views: 44

Answers (1)

Ray
Ray

Reputation: 777

I checked your image, you have to use array index for that

mydata[0][0].title 

success: function (data) {
    var mydata= JSON.parse(data); // mydata is JSON array        
}

Upvotes: 1

Related Questions