0

JSON is quite confusing and I haven't been able to find a post that can help me so I decided to ask the question myself.

Quick Summary

I need my php array to show up as [object Object] for every array containing an id, label,and type -- when passed through to my JavaScript using json. Here is what I've tried:

if( $node_id == NULL ) {
echo json_encode(array(
    0 => array( array("id" => 1, "label" => "A lunch", "type" => "folder"), array("id" => 2, "label" => "A lunch", "type" => "folder"))
));
}

When I run this code I keep getting my alert to display:

{"0":{"0":{"id":1,"label":"A lunch","type":"folder"},"1":{"id":2,"label":"A lunch","type":"folder"}}}

when it should look like

[object Object],[object Object]

so could someone please help convert it to that format through json, or however it might be done?

2 Answers 2

1

You're alerting a string, so it seems that jQuery is not recognizing your response as JSON, and it is not being parsed. Try this:

$.ajax({
    url : "php/resource.php", 
    data : { node_id: node_id },
    dataType : "json"
}).done(function(data) {
    alert(data);
    return callback(null, data);
}).fail(function() {
    return callback("AJAX error");
});
Sign up to request clarification or add additional context in comments.

2 Comments

It is now being returned as [object Object]!!!! but its not displaying so I'm guessing its not being properly set in the json
Jk i was trying to use JSON_FORCE_OBJECT and accidentally left it there. Once removed, instant fix! Thank you so much
1

Also, you may set content type to application/json in your php

header('Content-Type','application/json');

2 Comments

I had tried that earlier but it didn't work. But Thank you for the effort!
Weird... that always worked for me. Anyway, seems like you already got a solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.