Okay, I'm at a loss. I'm working though a jQuery for Drupal book. It's using Views Datasource to create a JSON page, and then using jQuery.getJSON function to grab information. All is well and good.
Here's important code:
jQuery.getJSON(url, function (data, result) {
if (result != 'success') {
return;
}
var comment = data[0];
Okay, here's the problem - data[0] doesn't exist. Comment remains null and unaccessible. Fine, I figure the book is a bit outdated, the JSON data it shows me isn't exactly what I had either, no big deal. I'll just run my way through the array and grab what I need. Except, for whatever reason, I can't select deeper into the array.
Here's how the JSON data is displayed:
{
"nodes" : [
{
"node" : {
"ID" : "13",
"Author" : "retsage",
"Body" : "NEWEST OF ALL!",
"Link" : "view",
"Title" : "Node 3"
}
},
{
"node" : {
"ID" : "12",
"Author" : "retsage",
"Body" : "New comment 2, monsieur.",
"Link" : "view",
"Title" : "Node 2"
}
},
{
"node" : {
"ID" : "11",
"Author" : "retsage",
"Body" : "Newest comments are fun!",
"Link" : "view",
"Title" : "Node 3"
}
}
]
}
But, I can't traverse through this array. I don't understand what's going on. If I try to pass data['nodes'] to a new variable, the result is null. If I try to pass data.nodes, the result is null. Very, very confused. Help please.
Comments
Not really sure what you are
Not really sure what you are asking. But from the description of your object, it sounds like data.nodes[0] should exist.
But you never really asked a question, or told us what you are trying to do. Tell us what you are trying to do and show the code that is not working.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
No worries, figured it out.
No worries, figured it out. data.nodes[0] does exist, not sure what was going on before. Sorry for confusion, was just very, very frustrated when I wrote this post.
For whatever reason, though, I can't pass just data.nodes (to grab the 0, 1, 2 objects). That's where my "what's-going-on" moment started from.