Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JSON - Javascript Iterate through object and display results

Options
  • 01-11-2012 5:47pm
    #1
    Registered Users Posts: 1,645 ✭✭✭


    I have $.getJSON returning this object from a database. Try as I have I can't get the call back function to iterate through it so I can display in my html.

    Edit: I'm obviously just looking to display just the value and not key
    {
    list: [
    {
    customer_name: "Joe Blogs"
    },
    {
    customer_name: "John Ryan"
    },
    {
    customer_name: "Ted McTeddy"
    },
    {
    customer_name: "Jane Doe"
    },
    {
    customer_name: "Mark Stokes"
    }
    ]
    }
    
    

    Any help would be appreciated, new to javascript, jquery etc so as much help as you can give me would be great.

    Cheers guys


Comments

  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic




  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    What about using for/in?
    for (var i in somedata.list)
    {
        alert(somedata.list[i].customer_name);
    }
    


  • Banned (with Prison Access) Posts: 1,332 ✭✭✭desaparecidos


    That JSON isn't valid. The keys need to be in double quotes too.

    Is the JSON located in the same domain as the page your requesting it from? If not then you need to use JSONp.

    Don't use a for/in without checking each property with hasOwnProperty. Much safer to use a standard for loop.

    Example here: http://jsfiddle.net/tThFy/2/

    Just un-comment the getJSON parts for use where you're actually getting the JSON from the server.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    You can use http://jsonlint.com/ to make sure your JSON is valid. You shouldn't be manually building a JSON string since any decent language/framework will have JSON support or external libraries that do it for you.

    Using a tool such as Firebug or Chrome Developer Tools will allow you to see if an exception is being thrown in javascript. They will also allow you to debug your javascript by setting breakpoints in the script.


  • Registered Users Posts: 1,645 ✭✭✭k.p.h


    That JSON isn't valid. The keys need to be in double quotes too.

    Is the JSON located in the same domain as the page your requesting it from? If not then you need to use JSONp.

    Don't use a for/in without checking each property with hasOwnProperty. Much safer to use a standard for loop.

    Example here: http://jsfiddle.net/tThFy/2/

    Just un-comment the getJSON parts for use where you're actually getting the JSON from the server.

    Sorry, the JSON is actually in double quotes I was just using JSONView in Chrome to look at the data.

    Everything else is a great help so I'l get into it for the morning and see if I can get it going.

    If I run into more errors I'l post back here and see if ye guys can help.

    Basically I have PHP scripts returning JSON from the server which worked great when I was working with native Android but I'm attempting to go multiplatform with PhoneGap which requires me to use jQuery Mobile and all the associated tech, it would be great if I didn't have to rewrite the server scripts and just use what I have already. I'm sure i'l be back for more help..! Thanks ;)

    Edit: Just dropped in your code desaparecidos and it works like a charm, many thanks.. :)


  • Advertisement
Advertisement