dcsimg
www.webdeveloper.com
Results 1 to 12 of 12

Thread: JavaScript array to JSON

  1. #1
    Join Date
    Oct 2017
    Posts
    5

    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:

    HTML Code:
    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.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,257
    You need PHP's set of JSON functions to produce the JSON string that yo then parse in the browser.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,257
    but maybe you need to do this in JavaScript if you have collected this data on the clients machine.

    Its not clear.

    Where is the data originating from, the user or the server?
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Oct 2017
    Posts
    5
    By the time I want to create a JSON, I will have a full javascript array generated by the user interaction. I would just need to make a JSON file from it.

  5. #5
    Join Date
    Dec 2012
    Posts
    1,495
    You can hand over any javascript variable to stringify, it needs not be preset with fixed values:
    Code:
    var myJSON = JSON.stringify(your_variable);
    your_variable can be the array created by user interaction.

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,257
    The point I am making here is this JSON string, are you wanting to parse the string as in your server set an object that your clients browser parses with JSON.parse.

    Or

    Is your client inputting in to a form some information that you want to stringify to send to the server?

    You have not made it clear what your objective is other than mention you want to take na array value and plug that in to the JSON object and then do something with the data.

    or is it something like this...
    Code:
    var obj = { "name":array[4].name, "age":30, "city":"New York"};
    var myJSON = JSON.stringify(obj);
    Is that what you mean?
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  7. #7
    Join Date
    Oct 2017
    Posts
    5
    Quote Originally Posted by \\.\ View Post
    The point I am making here is this JSON string, are you wanting to parse the string as in your server set an object that your clients browser parses with JSON.parse.

    Or

    Is your client inputting in to a form some information that you want to stringify to send to the server?

    You have not made it clear what your objective is other than mention you want to take na array value and plug that in to the JSON object and then do something with the data.

    or is it something like this...
    Code:
    var obj = { "name":array[4].name, "age":30, "city":"New York"};
    var myJSON = JSON.stringify(obj);
    Is that what you mean?
    Exactly as the example u said.
    What I want is this.

    I have an online calculator that I made for moving company. So basically, the user clicks on all the things they need moved (chairs, tables, sofas, beds etc), and when they click SEND, I get all that data via e-mail, something like this foe ex:
    Chairs - 5
    Tables - 2
    Beds - 4
    etc...

    I'm planning to use a contact form, so alongside the name,mail, phone, message, the json string will get attached aswell...
    Could be that there is a more simple way around this, if yes, I'd love to know, but it's more comfortable for me via this, as I'm a bit familiar. I don't know much of PHP :/

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,257
    I would use an object constructor function for each item you add to the data string... use a HTML form to collect the information and then have an array store the data and then stringify that before sending to the server for emailing to yourself.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  9. #9
    Join Date
    Oct 2017
    Posts
    5
    Hi guys,

    Got it working, I just need to know how can I make it look less ugly than this: ( ) , 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']

  10. #10
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,257
    Sounds more like you have no code experience because $_POST array is PHP which runs on the server.

    If you need data from a user sent to a server, then use a web form (which is HTML not JavaScript).

    If you need help with something then more than two lines of code is going to be needed because each element is like a layer that interact with the previous layer...
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  11. #11
    Join Date
    Oct 2017
    Posts
    5
    Quote Originally Posted by \\.\ View Post
    Sounds more like you have no code experience because $_POST array is PHP which runs on the server.

    If you need data from a user sent to a server, then use a web form (which is HTML not JavaScript).

    If you need help with something then more than two lines of code is going to be needed because each element is like a layer that interact with the previous layer...

    Yeah right, I know. I'm using my hosting for that, and sending mail seems to be working nice. Now I only need to attach my JSON array with the mail in plain text. Could someone please show me how?

  12. #12
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,257
    You have to let people know how you have coded your site, like are you using a web form and if so, are you using it correctly, server side script handler for the incoming data stream... you mention $_POST array but that relates to actual web form inputs...

    So you need to post the HTML and PHP you are using as well as any other elements of code you are using and please post your code in forum BBCode tags.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles