Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    jquery decode json object with double quoted keys

    I have a script that outputs a json string via json_encode in PHP. The json string consists of

    PHP Code:
    [{"custId":"2","custName":"John Inc"}] 
    The PHP script is initiated using

    Code:
    $.getJSON("customer.php", function(data){alert(data + '  ' + data.custName);});
    The response is -

    Code:
    [object Object] undefined
    Javascript recognises 'data' as an object but I cannot seem to reference the information using json dotted notation.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    try using
    Code:
    alert(data.toSource());
    in firefox, and see what data actually is.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    New Coder
    Join Date
    Sep 2004
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Work it out that I had braces surrounding my json string therefore it was being interpreted as array(object).

    Fixed it with data[0].custName

    Cheers


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •