JavaScript array to JSON

Hey guys,

I have a JavaScript array, which I want to convert to JSON.
The thing is that I don’t know what will be in the array, cause it’s a calculator, that depends on the user.

I saw some commands that parse js to json, but the thing is that I want to be able to use a VARIABLE instead of a fixed input.

Let’s take this as an example:

var obj = { "name":"John", "age":30, "city":"New York"};
var myJSON = JSON.stringify(obj);

I want to be able instead of “John”, or “30”, or “New York”, to be able to use a var. let’s say array[4].name or anything, cause I don’t know those values.

JS object literals allow the use of variables for defining the values.

1 Like

I don’t get it sry, can u please be more specific, I’d also appreciate an example a lot.


    function check_answer(e) {
        e.preventDefault();
        //console.log("e.target", e.target);
        var answer = $(e.target).data('answer');
        var params = {id: id, answer: answer}; // You don't need quotes on the left side of the : 
        var myData = jQuery.param(params); // Set parameters to correct Ajax format:
        check_answer_ajax(myData);
    } // End of check_answer function:

    function check_answer_ajax(myData) {
        $checkAns.off('click', check_answer);
        $.ajax({
            type: 'post',
            url: 'game_play_01.php',
            data: myData,
            success: function (result) {

I think the above is a JQuery example is more what you’re looking for?

function main(url, start = 0, end = 56, category = 'movie', callback) {

    console.log(start, end, category);
    var check = 'start=' + start + '&end=' + end + '&category=' + category;

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 2) {
            //console.log(xhr.status);
        }
        if (xhr.readyState === 4 && xhr.status === 200) {
            callback.call(xhr.responseXML);
        }
    }; // End of Ready State:
    xhr.open('POST', url, true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    xhr.send(check);
}

This is a simplify way of doing something like that in vanilla javascript.

Hi guys,

Got it working, I just need to know how can I make it look less ugly than this: ( http://prntscr.com/gxc2vd ) , and any ideas how I can attach this alongside the other mail infos (name,mail,phone, message), when they’ll hit SEND. I heard I can use something like:
$_POST[‘myarray’]

JSON cannot look less ugly, for it’s a data transport system that isn’t designed to be presented on the screen.
You can hide it away though, by putting it in a hidden form field.

<input type="hidden" name="orderAmounts">

And then later in the script:

form.elements.orderAmounts.value = myJSON;

That hidden form field will then be submitted along with the rest of the form.

That’s great and simple.
How about a use a more readable way? Any ideas? I want every element of the array to be shown in a new line when it arrives the Inbox of the receiver.

That’a something that PHP is better suited at doing when it receives the form.

Is there such thing like PHP array or anything else you would suggest I do with the JavaScript array? Maybe pass it to php array (if they exist lol), and then pass that array to the mail

Nope, the JSON format has been designed for the process of transmitting the info. When PHP receives it, there are PHP methods that turn it in to an array and you can split it up from there.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.