tags:

views:

44

answers:

3

I'm having a problem trying to return a specific record using jQuery and JSON. I'm passing a variable to the function and want to output only the data matching the id I've set when the function is executed.

Right now, my function looks like this:

function getEffectsData(myNum){
    $.getJSON("jsonscript.php", { id: +myNum },function(data){
       var x = data.x;
       //DO SOME STUFF
    });
}

The JSON output looks like this:

{"stuff": [ { "id":1, "x":3, "y":6, "z":-6 },{ "id":2, "x":2, "y":7, "z":-3 }]}

The only thing I know is that the correct variable is being passed to the function. After that, nothing happens. Very new at this, so any help is greatly appreciated.

EDIT: I appreciate everybody's input on this. To clarify, I am trying to return the data from only the object that's id matches myNum. I want to be able to access all of the data from that object, but only that object. I have no idea where that object will be in the array. I simplified the ids and the data in my question to help clarify the problem.

Thanks again.

A: 

should be

var x = data[0].x;

or

var x = data[1]['x'];

for instance.

jAndy
Actually, @Nick's answer is right. There's a `stuff` property that has an array value. Each of the objects in this array have an `x` property.
tvanfosson
+3  A: 

You can access x from your JSON like this:

var x1 = data.stuff[0].x; // the first object's x key
var x2 = data.stuff[1].x; // the second object's x key

Because your JSON tree looks like this:

{ // base object
    "stuff": [ // array
    { // 0.: object
        "id": 1,  // key => value
        "x" : 3,  // key => value
        "y" : 6,  // key => value
        "z" : -6  // key => value
    },
    { // 1.: object
        "id": 2,  // key => value
        "x" : 2,  // key => value
        "y" : 7,  // key => value
        "z" : -3  // key => value
    }]
}

So with data.stuff[0].x you select stuff object then it's first element and then its x key.

But If you want to handle all the x key for example, then you need to loop through the stuff array by a simple for loop or the $.each method.


UPDATE

As for your question. If you want to get the object with id myNum you have 2 possibilities:

  1. if you possess jsonscript.php you can send the data only with the correct id from the server, because you pass the id to it by { id: +myNum } which is the second parameter of getJSON

  2. You loop through the data until you find the object with the correct id

    var object; 
    $.each( data.stuff, function(i, obj) {
      if (obj.id === myNum) object = obj;
    };
    // now object is the one with the right id
    
galambalazs
+1 nice explanation of the object structure.
tvanfosson
I'm only trying to access the object with the same id that matches myNum. I have no idea where it will be within the array as the actual ids are non-sequential.
Duffy Dolan
How do I pass the JS variable myNum to the PHP mysql query? I'm making the (probably wrong) assumption that it would be "WHERE id = myNum". This is what I'm trying to do though.
Duffy Dolan
Using the if statement works perfectly fine. Thanks!
Duffy Dolan
you're welcome :)
galambalazs
+1  A: 

It's an array under stuff, so you would need something like this:

function getEffectsData(myNum){
    $.getJSON("jsonscript.php", { id: +myNum },function(data){
       $.each(data.stuff, function(i, obj) {
         var x = obj.x;
         //DO SOME STUFF
       });
    });
}

If you're after the very first x you can do data.stuff[0].x, but I think you'd want to loop through here.

Nick Craver
+1 for the right way to do it.
tvanfosson