Grow your CSS skills. Land your dream job.

[Solved] JQuery and JSON help

  • # February 7, 2010 at 11:59 am

    I hope someone can help me, I have an AJAX call which loads some data from a database and returns it as JSON. The code I use is:

    Code:
    $.ajax({
    type: “POST”,
    url: “ajaxhandler.php”,
    data: “action=svgjson”,
    datatype: “json”,
    success: function(json){
    $(‘#searchResults’).html(json);
    }
    });

    This works fine and returns the JSON and dumps the lot in the browser. However, I am having trouble parsing the file and getting the details I want from it. The file is structured as so:

    Code:
    {
    “floorplan”:[
    {
    “id”:”1″,
    “room”:”9000″,
    “use”:”Gallery”,
    “category”:”Gallery”,
    “colour”:”#FFFF22″
    },
    {
    “id”:”3″,
    “room”:”9001″,
    “use”:”Store Room”,
    “category”:”Utility”,
    “colour”:”#000000″
    }]}

    Say I want to get the colour for each floorplan item. How do I go about doing this? I was thinking something like:

    Code:
    $(‘#searchResults’).html(json.floorplan.colour);

    But it just returns the error

    Code:
    TypeError: Result of expression ‘json.floorplan’ [undefined] is not an object.

    Any help is much appreciated!

    EDIT: Never mind, fixed it myself. Apparently it was returning a string not an object so I had to use the eval() function to convert the string to an object. Now works perfectly!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".