0

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.

Duffy Dolan
  • 392
  • 3
  • 8
  • 19
  • 1
    uh...isn't +myNum a syntax error? – sje397 Jul 08 '10 at 12:20
  • What exactly do you want? Which data do you want to access from the JSON string? `data.x` is obviously wrong. – Felix Kling Jul 08 '10 at 12:20
  • @sje397: If `myNum` is a number (not a string or object) then NO. It is just math. `-myNum` wouldn't be an error either. – Felix Kling Jul 08 '10 at 12:22
  • @Felix ah yeah...i'd blame the vodka but i think it's more that it's such an uncommon (and, i'll add, useless) syntax :) thanks – sje397 Jul 08 '10 at 12:28
  • I want to access only the data that matches the id set with myNum. So if myNum = 2, I want only the data from the object with the id of 2. Does that clarify anything? – Duffy Dolan Jul 08 '10 at 13:36

3 Answers3

6

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
    
gblazex
  • 49,155
  • 12
  • 98
  • 91
  • 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 Jul 08 '10 at 13:38
  • 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 Jul 08 '10 at 14:06
  • Why do I get `Unexpected token ;` on this? – Chase Jul 13 '16 at 17:31
  • there was a ')' missing at the end of step **2.** – gblazex Jul 13 '16 at 22:31
1

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
  • 623,446
  • 136
  • 1,297
  • 1,155
0

should be

var x = data[0].x;

or

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

for instance.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 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 Jul 08 '10 at 12:24