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
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts

    return json output without square brakets

    I am returning the following through JSON:

    Code:
    $this->view->lead_query=$lead_query->fetchALL();
    header('OK', true, 200);
    header('Content-type: application/json');
    echo json_encode($this->view->lead_query);
    I am getting the results in this format:
    [{"client_name":"Steve Harris","state":"TX"}]

    Now if the above output was displayed without the square brackets [] I can populate the values in my form correctly.
    Is there a way i can return the output without the square brackets?

  • #2
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    PHP Code:
    str_replace( array( '['']'), ''json_encode($this->view->lead_query)) 
    the above worked for me

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Otherwise you could as well process the returned JSON in Javascript as an array rather than as an object.

    You possibly tried
    Code:
    myobject.client_name
    and you can do this instead
    Code:
    myobject[0].client_name


  •  

    Posting Permissions

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