Ok, I really don't get on with JSON stuff?

I've managed to get a JSON output from my database for use with jQuery UI AutoComplete, and that's working fine.

The trouble with it is, it seems the AutoComoplete plugin does all the parsing of JSON data for me, so I'm still to work out how that's actually done.

I have a URL, http://test.hartnollguitars.co.uk/jsonOut/products.aspx?term=761294042656&barcode=yes (as an example) which gives me a single record from the same JSON output as I use for Autocomplete, I am trying to use this single record output for a single case.

So I've been Googling and messing around with jQuery getJSON, but I seem unable to get the info.

$.getJSON("jsonOut/products.aspx?barcode=yes", function () { alert(data.label);  });

Is kind of where I'm at now, I know this is wrong and I have tried various other permutations of this code following examples from the jQuery site, but I just can't get the output I need.

It's obviously because I've got no idea what I am doing, but can someone just explain how you:

  1. Get data from a single record like this
  2. Get data from a series of data and loop through to display the output (for example from an FQL output from the Facebook Graph for a simple object such as events)

All help much appreciated!

Answers


Remeber to use an absolute url, not a relative. The code you have pasted will append the url to the current directory, so it will only work if it is coming from /. The JSON being returned is an array, so you have to dereference an element:

$.getJSON("/jsonOut/products.aspx?barcode=yes", function (data) { alert(data[0].label);  });

or the more useful thing to do would be:

$.getJSON("/jsonOut/products.aspx?barcode=yes", function (data) {
   for (var i = 0; i < data.length; ++i) {
      alert(data[i].label);
   }
});

data does not come from nowhere, its a parameter to your success function

$.getJSON(
     "jsonOut/products.aspx?barcode=yes", 
     function (data) { 
         alert(data.label);  
});

And you output an array

[
   {
        "label": "Boss TR-2 Tremolo Pedal",
        "price": 79,
        "id": 1287
    }
]

Thats what the '[]' braches stand for, so your data can be accessed in another way

data[0].label

Please consider developing in Firefox and using FireBug, then you could write

console.log(data);

instead of alert(), wich will give you much greater insight into your objects.


I looked at your JSON Output: [{"label":"Boss TR-2 Tremolo Pedal","price":79,"id":1287}]

The Problem is, that your Code outputs a List of Objects. Try data[0].label instead.


Need Your Help

Create AMF wrapper

flex amf zend-amf flex-mobile

I am creating a mobile app that will connect to a zendamf implementation to retrive certain information to store and display to the user.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.