Retrieve json_encode values in javascript array

Posted on 2009-12-23
Last Modified: 2013-11-08
I am using following PHP code to store column values and pass to JSON function:

echo json_encode(array($PostedDate.$Places.$Company.$Designation.$ProjectDetails.$DesiredCandidate.$HRName.$HRContact.$Email));

The above thing is returned in a variable called 'data' to JSON function. I want to store the above values in JavsScript array? How to do that?
Question by:rpkhare
    LVL 8

    Expert Comment

    The point of JSON is to have them stored as JS objects.

    So if you have in your JavaScript:

    var data = {"a":1, "b":2, "c":3};

    You can then access it as such:


    Let me know if that is fine. If you really want a JS array, you can loop over the array instead of running a json_encode() over it and output it with JS array syntax.
    LVL 8

    Author Comment

    Following code calls the code I mentioned in my original post. Please let me know how to modify it?

    							$.getJSON("fetchvalues.php?UpdateRecordID=" + $.cookie('UpdateRecordID'),
    							// Data retrieved in concatenated form. So we will break it and store values in array.
    							var concatenatedvalues = new Array();
    							concatenatedValues = data;

    Open in new window

    LVL 8

    Accepted Solution

    Ok, I solved this problem. Someone suggested me to use commas instead of period is this code:

    echo json_encode(array($PostedDate.$Places.$Company.$Designation.$ProjectDetails.$DesiredCandidate.$HRName.$HRContact.$Email));

    Now, from within I can call a column simply like:


    Please note that the data is a function used by JSON to retrieve results.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now